Animation at Button Press

Hi Everyone,

I'm sure there's something that covers this out there, but I struggled to find it because I'm not sure what exactly to search for.

Anyway, I'm working on a part of my game where the player is in a sewer and there's aggressive rats that will chase you if they see you. I'd like to set an event where you can press a button and play a specific animation against one of the rats which would knock them back and/or stun them.

Similar to an ABS (action battle system) but I only want it to be done in one part of the map not the whole game.

Any ideas on how this could be done with events?

Thanks for your help!
The following Eventcode +Eventcode Scriptsnippet allows you to Hit the Events 3 to 100, Hitrange is one Tile infront of the Player, if Hit: The Self Switch A of that Event is turned On.
Recreate the following as parallel Map Event on Highest Frequenzy, it will check for the Events 3 to 100. Maybe there need some adjustments to be done, let us know.

Code:
◆If:Button [Pagedown] is pressed down
  ◆Control Variables:#0001 Sword Slash X = 0
  ◆Control Variables:#0002 Sword Slash Y = 0
  ◆If:Player is facing Down
    ◆Show Animation:Player, <3> Slash Down
    ◆Control Variables:#0002 Sword Slash Y = 48
    ◆
  :End
  ◆If:Player is facing Left
    ◆Show Animation:Player, <3> Slash Left
    ◆Control Variables:#0001 Sword Slash X = -48
    ◆
  :End
  ◆If:Player is facing Right
    ◆Show Animation:Player, <3> Slash Right
    ◆Control Variables:#0001 Sword Slash X = 48
    ◆
  :End
  ◆If:Player is facing Up
    ◆Show Animation:Player, <3> Slash Up
    ◆Control Variables:#0002 Sword Slash Y = -48
    ◆
  :End
  ◆Script:var hitDist = 0;  // just for readability
  :      :for (var a = 3; a <= 100; a++) {
  :      :  if (!!$gameMap.event(a)) {   // check that event exists
  :      :    hitDist = Math.hypot($gameMap.event(a).screenX() - ($gamePlayer.screenX() + $gameVariables.value(1))   ,
  :      :                         $gameMap.event(a).screenY() - ($gamePlayer.screenY() + $gameVariables.value(2)) );
  :      :    if (hitDist <= 30) {  // in range? in pixel radius.
  :      :
  :      :        $gameSelfSwitches.setValue([$gameMap.mapId(), a, 'A'], true)
  :      :      } } }
  ◆Loop
    ◆If:Button [Pagedown] is pressed down
      ◆Wait:1 frame
      ◆
    :Else
     ◆ Wait 4 Frames //Wait more Frames if you got long Animation
      ◆Break Loop
      ◆
    :End
    ◆
  :Repeat Above
  ◆
:End
The following Eventcode +Eventcode Scriptsnippet allows you to Hit the Events 3 to 100, Hitrange is one Tile infront of the Player, if Hit: The Self Switch A of that Event is turned On.
Recreate the following as parallel Map Event on Highest Frequenzy, it will check for the Events 3 to 100. Maybe there need some adjustments to be done, let us know.

Code:
◆If:Button [Pagedown] is pressed down
  ◆Control Variables:#0001 Sword Slash X = 0
  ◆Control Variables:#0002 Sword Slash Y = 0
  ◆If:Player is facing Down
    ◆Show Animation:Player, <3> Slash Down
    ◆Control Variables:#0002 Sword Slash Y = 48
    ◆
  :End
  ◆If:Player is facing Left
    ◆Show Animation:Player, <3> Slash Left
    ◆Control Variables:#0001 Sword Slash X = -48
    ◆
  :End
  ◆If:Player is facing Right
    ◆Show Animation:Player, <3> Slash Right
    ◆Control Variables:#0001 Sword Slash X = 48
    ◆
  :End
  ◆If:Player is facing Up
    ◆Show Animation:Player, <3> Slash Up
    ◆Control Variables:#0002 Sword Slash Y = -48
    ◆
  :End
  ◆Script:var hitDist = 0;  // just for readability
  :      :for (var a = 3; a <= 100; a++) {
  :      :  if (!!$gameMap.event(a)) {   // check that event exists
  :      :    hitDist = Math.hypot($gameMap.event(a).screenX() - ($gamePlayer.screenX() + $gameVariables.value(1))   ,
  :      :                         $gameMap.event(a).screenY() - ($gamePlayer.screenY() + $gameVariables.value(2)) );
  :      :    if (hitDist <= 30) {  // in range? in pixel radius.
  :      :
  :      :        $gameSelfSwitches.setValue([$gameMap.mapId(), a, 'A'], true)
  :      :      } } }
  ◆Loop
    ◆If:Button [Pagedown] is pressed down
      ◆Wait:1 frame
      ◆
    :Else
     ◆ Wait 4 Frames //Wait more Frames if you got long Animation
      ◆Break Loop
      ◆
    :End
    ◆
  :Repeat Above
  ◆
:End

This is great! Thanks so much. I'll test this a bit and see if I can make it work.
It is used in a Parallel Event, the first ones are Event Commands, the last one that reads Script, is Eventcommand "Script" on page 3 of the Event Commands.
Caethyril build most of that for me, i just shared it across some Threads.

Original thread: https://forums.rpgmakerweb.com/threads/animation-at-button-press.105408/