Detect Keydown and Key release!

AaronTrip

Villager
Member
Joined
Sep 27, 2018
Messages
18
Reaction score
0
First Language
English
Primarily Uses
RMMV
Hi, Im pretty new to RPG maker MV and im a little stuck right now.
Basically what I want to do is for the game be able to detect when the keys J, K and L are being pressed down, and also when they are released. The Mechanic for my game is color changing so in short what I want is when a key is being held for screen to tint, and when its released the screen will to go back to normal. Any info helps! Thanks.
 

AaronTrip

Villager
Member
Joined
Sep 27, 2018
Messages
18
Reaction score
0
First Language
English
Primarily Uses
RMMV
Thank you for the reply! This looks like a plugin that supports touch screen commands! Im looking to use the in game tint function on keyboard commands!

Unless the plugin allows me to re-map its feature to keys?
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
The engine only recognizes a certain amount of keys by default. You can look into the spoiler to see which keys it recognizes with just the base code:
Code:
Input.keyMapper = {
    9: 'tab',       // tab
    13: 'ok',       // enter
    16: 'shift',    // shift
    17: 'control',  // control
    18: 'control',  // alt
    27: 'escape',   // escape
    32: 'ok',       // space
    33: 'pageup',   // pageup
    34: 'pagedown', // pagedown
    37: 'left',     // left arrow
    38: 'up',       // up arrow
    39: 'right',    // right arrow
    40: 'down',     // down arrow
    45: 'escape',   // insert
    81: 'pageup',   // Q
    87: 'pagedown', // W
    88: 'escape',   // X
    90: 'ok',       // Z
    96: 'escape',   // numpad 0
    98: 'down',     // numpad 2
    100: 'left',    // numpad 4
    102: 'right',   // numpad 6
    104: 'up',      // numpad 8
    120: 'debug'    // F9
};

Fortunately, it's easy to add more keys that it recognizes.

1.) We first check a table to see what code corresponds to J, K, and L. The corresponding number codes are 74, 75, and 76.

2.) Then we can add those keys to the mapping that the engine recognizes:
Code:
Input.keyMapper[74] = 'J';
Input.keyMapper[75] = 'K';
Input.keyMapper[76] = 'L';
^^ You can add this code as a new "plugin".

3.) Now, inside a parallel event (it can be on the map, or a Common Event), you can check if these keys are pressed down.

You can use the Conditional Branch command with the script option. The following script checks are relevant for you.

Code:
Input.isTriggered('J')
// This is true if J was just pressed, false if J was not just pressed
Code:
Input.isPressed('K')
// This is true if K is currently pressed, false if K is not currently pressed
Code:
Input.isLongPressed('L')
// This is true if L has been pressed for longer than 24 frames, false if L has not been pressed for longer than 24 frames
If you want to check the opposite, i.e. check if a button is NOT pressed, just put an exclamation mark at the front.
Code:
!Input.isLongPressed('J')
4.) Construct your event. Remember that parallel events run every single frame, so you want to use some switches so that you don't lag your game.

Something like (these are pseudo-commands, you have to translate it to event commands)

Code:
Conditional Branch: If the Tint Switch is OFF:
    Conditional Branch: If 'K' is Long Pressed:
        Tint the screen
        Turn ON the Tint Switch
Conditional Branch: If the Tint Switch is ON:
    Conditional Branch: If 'K' is NOT Long Pressed:
        Tint the screen back to normal
        Turn OFF the Tint Switch
You'll have to test it and tweak it, maybe add some Wait commands, but that's the idea.
 

AaronTrip

Villager
Member
Joined
Sep 27, 2018
Messages
18
Reaction score
0
First Language
English
Primarily Uses
RMMV
Awesome info! thank you! so when adding those key mappers as a plugin do i just have to copy that code into a js file and put it in the plugins folder? then just turn it on?
 

Aloe Guvner

Walrus
Veteran
Joined
Sep 28, 2017
Messages
1,628
Reaction score
1,115
First Language
English
Primarily Uses
RMMV
Awesome info! thank you! so when adding those key mappers as a plugin do i just have to copy that code into a js file and put it in the plugins folder? then just turn it on?
Exactly.
 

AaronTrip

Villager
Member
Joined
Sep 27, 2018
Messages
18
Reaction score
0
First Language
English
Primarily Uses
RMMV
awesome, so I've got it working that when you press the button down it will tint the screen, and when you release it will go back to normal. I've noticed now though that if the button is held down for longer than a few seconds, it still goes back to normal no matter what. Im wondering if there is a way to get the screen to just wait until you release the button?
 

AaronTrip

Villager
Member
Joined
Sep 27, 2018
Messages
18
Reaction score
0
First Language
English
Primarily Uses
RMMV
actually, i think i figured it out, so far at least hahah. Thanks again!
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,045
Members
137,569
Latest member
Shtelsky
Top