TM.InputManager_Keyboard

View source code : https://github.com/a-mean-blogger/text-game-maker-js/blob/master/src/managers/input-manager/input-manager_keyboard.js

Type

Class (extends TM.IObject)

**This class is made to use as an sub property of TM.InputManager. so that's why the class name start with InputManager_

Purpose

A class that creates instances capture keyboard inputs. The keyboard property of TM.InputManager's instance is the instance of this class.

Important Properties

  • isActive: It will be set true after calling init function, false after calling inactivate. The Object status(active/inactive) can be checked by this property.

Important Functions

  • init(): activates/initializes the instance. (It executes when the instance created)
  • inactivate(): inactivates the instance. User's keyboard inputs will not be captured.

Functions related to Key State

Key state in Game Maker JS means if a key is pressed at the moment of function call.

  • checkKeyState(keyCode): checks if the key for keyCode is currently pressed.
  • checkKeyStateAny(): checks if any key is currently pressed.
  • removeKeyState(keyCode): removes the key state information of keyCode.
  • clearKeyState(): removes all key state information.

Functions related to Key Pressed

Key pressed in Text Game Maker JS means if a key has been pressed since last key press check

  • checkKeyPressed(keyCode): checks if the key for keyCode has been pressed since last check.
  • checkKeyPressedAny(): checks if any key has been pressed since last check.
  • removeKeyPressed(keyCode): removes the key pressed information of keyCode.
  • clearKeyPressed(): removes all key pressed information.

Functions related to Key

The functions check  key state and key pressed.

  • checkKey(keyCode): checks if the key for keyCode has been pressed since last check or if the key for keyCode is currently pressed.
  • checkKeyAny(): checks if any key has been pressed since last check or if any key is currently pressed.
  • removeKey(keyCode): removes both key pressed and key state information of keyCode.
  • clearKey(): removes every key pressed and key state information.

Other functions

  • getInput(question): pops up an dialog with question text on the browser and get your input.

Code Example

**View example source code: input-manager_keyboard-tutorial.js

First, create an instance of TM.ScreenManager and assign to var TMS(Text Game Maker Screen) and then also create an instance of TM.ScreenManager and assign to var TMI(Text Game Maker Inputs). TM.InputManager_Keyboard's instance will be created and assign to TMI.keyboard.

var TMS = new TM.ScreenManager(),
    TMI = new TM.InputManager(null, true);

create KEYSET object that contains key codes. You can get the key codes when you press keys on the browser console if you create create TMI with TMI = new TM.InputManager(null,true);

var KEYSET = {
  A: 65,
  S: 83,
  D: 68,
};

Create a TM.Interval's instance that will check keys every 200 milliseconds
pressing 'A' key will be checked by checkKeyState function,
pressing 'S' key will be checked by checkKeyPressed function,
pressing 'D' key will be checked by checkKey.

var myInputMgr = new TM.Interval(200,function(){
  var textA = "TMI.keyboard.checkKeyState(KEYSET.A)";
  if(TMI.keyboard.checkKeyState(KEYSET.A)) TMS.insertTextAt(1,1,textA);
  else TMS.deleteTextAt(1,1,textA);

  var textS = "TMI.keyboard.checkKeyPressed(KEYSET.S)";
  if(TMI.keyboard.checkKeyPressed(KEYSET.S)) TMS.insertTextAt(1,2,textS);
  else TMS.deleteTextAt(1,2,textS);

  var textD = "TMI.keyboard.checkKey(KEYSET.D)";
  if(TMI.keyboard.checkKey(KEYSET.D)) TMS.insertTextAt(1,3,textD);
  else TMS.deleteTextAt(1,3,textD);
})
myInputMgr.init();

Run Example

Click Screen first, then your key input will be checked.

Type these commands into the browser console on this page to test.
If you cannot open browser console with keyboard shortcut, click every where else but Screen and then you can open it. Screen captures all key input and prevent other key actions.

//re-activate(initialize) inactivate TMI.keyboard 
TMI.keyboard.inactivate();

//TMI.keyboard
TMI.keyboard.init();

You can see it print key code on the browser console when you press any keys so you can get key code.

from this example, you can see the difference between checkKeyState, checkKeyPressed and  checkKey functions.

A key, checked by checkKeyState function - It will not show texts on Screen if you press A key fast enough. It's because the code checks A key with checkKeyPressed function every 200 milliseconds interval, which return false if A key is not pressed at the moment of checking, .

S key, checked by checkKeyPressed function - It always show text on Screen even if you press S key very fast. It's because the code checks S key with checkKeyPressed function which check if S key has been ever pressed after last check. However, if you keep pressed down S key, the text appear first and then disappear and then appear again. it's because when you press a key down long, browser adds some gap time in between the at the first.

D key, checked by checkKey  - it will always show text on Screen when you press D key fast or press down long.

Comments

Add Comment

Login with SNS account to write comments. see details

UP