The Official Blog of Patcoola

Im just a dude watching over my parents,
and trying to make cartoons and video games from home.

Script: Key [JS API]

Important!

Dear visitors,

This blog and the Tigarus.com network will be changing our website software in May (2024). There will be some down time and there will be a change to the webpage addresses.

We will also be removing the Anime blogs. The Rocksmith Songs website will be unavailable of a few weeks or months while I move the database to the new software.

At the moment we are running on Wordpress, the new software is an in-house project which is 57 times faster. It will also provide more freedom for development.

Thank you for understanding.

, By Patcoola

Script Title: key
Programming Language: Java Script
Date Completed: 2012/09/24

Purpose: To provide an easy to use API for handling Keyboard keys.

Advertisement

Donate Now!

This might not be the best time, but making a blog is not easy. Would you consider donating?


Description: Handling and managing Keyboard key events for application or game development can be difficult with so many factors to consider. Key simplifies the process into a simple system. Easily determine when a key is down or up, disable default key actions, enable or disable keys, restrict input keys for forms and many more.

Compatibility: All modern Web browsers

Note: Some Web browsers such as Opera and Internet Explorer will not allow Java Script to disable the browser’s important actions such as Alt+F4 or Ctrl+P.

Methods:

Functions
key.isDown( int );Determines if a key is down, returns true or false
key.isUp( int );Determines if a key is up, returns true or false
key.isCaps();Returns true if a key pressed is a Caps Lock Event
key.addKey( int );Adds a key to the key listeners
key.removeKey( int );Removes a key from the key listeners
key.clear(); Clears all keys from the key listeners
key.start(); Starts key listeners
key.stop();Stops key listeners
key.disableKey( int );Sets a key to disabled
key.enableKey( int );Enables a key that was disabled
key.restoreKeys();Restores all keys that was disabled

Variables
key.onPress;Returns the value of the last key pressed
key.onDown;Returns the value of the last key down
key.isActive;Returns true or false if the key listeners are active
key.disableContextMenu;Set or Returns true if menu is disabled
key.disableCtrlKey;Set or Returns true if Ctrl is disabled
key.disableShiftKey;Set or Returns true if Shift is disabled
key.disableAltKey;Set or Returns true if Alt is disabled
key.disableCmdKey;Set or Returns true if CMD is disabled
key.disableFunctions;Set or Returns true if function keys are disabled
key.disableAllKeys;Set or Returns true if all keys are disabled

Valid if disabledAllKeys is set to true
key.allowUpperAlpha;Set or Returns true if uppercase letters are enabled
key.allowLowerAlpha;Set or Returns true if lowercase letters are enabled
key.allowDigits;Set or Returns true if digits are enabled
key.allowNumberPad;Set or Returns true if the number pad is enabled
key.allowSpecials;Set or Returns true if special character are enabled
key.allowFunctions;Set or Returns true if function keys are enabled

Disable Context Menu:

Default value false, when set to true the Context Menu key action is disabled.
Note: Available with Opera 12 and later.

Disable Control Key:

Default value false, when set to true the Ctrl+[Key] action is disabled. Example: (Ctrl+V) the paste command is disabled.

Disable Shift Key:

Default value false, when set to true the Shift key is disabled.

Disable Alt Key:

Default value false, when set to true the Alt key is disabled. Example: (Alt+F4) the exit program command is disabled.

Disable Command Key:

(Mac)
Default value false, when set to true the CMD key is disabled. This feature is for Apple computers.

Disable Function Keys:

Default value false, when set to true the F1-F12 keys’ actions are disabled. Works with the default F key functions, if F-Lock is on.

Disable All Keys:

Default value false, when set to true all keys default actions are disabled.
Use key.addKey() to enable a key, or enable a set of keys using an allow keys property.

Allow Upper Alpha:

Default value false, when set to true all uppercase letters are enabled.
Valid if key.disableAllKeys is set to true.

Allow Lower Alpha:

Default value false, when set to true all lowercase letters are enabled.
Valid if key.disableAllKeys is set to true.

Allow Digits:

Default value false, when set to true all digit keys are enabled (0-9), does not include number pad keys.
Valid if key.disableAllKeys is set to true.

Allow Number Pad:

Default value false, when set to true all numbers on the Number Pad are enabled (0-9).
Valid if key.disableAllKeys is set to true.

Allow Specials:

Default value false, when set to true all special characters are enabled.
Valid if key.disableAllKeys is set to true.

Allow Functions:

Default value false, when set to true all F1-F12 key actions are enabled.
Valid if key.disableAllKeys is set to true.

Example Usage:

// left arrow key

key.addKey(37);
key.start();

function onKeyEventDown() {
if ( key.isDown(37) ) {
// do something
}
}
document.addEventListener(‘keydown’, onKeyEventDown, false);

Tips:

// ctrl+a

if ( key.isDown(17) && key.isDown(65) ) {
// do something
}

// shift+a

if ( key.isDown(16) && key.isDown(65) ) {
// do something
}

Example Usage 2:

// left arrow key

key.addKey(37);
key.start();

function onKeyEventDown() {
if ( key.isDown(37) ) {
// do something
}
}

var intv = self.setInterval (“onKeyEventDown()”, 16);

 

Advertisement

Donate Now!

After a hard day, I like to relax with a cold jar of pickle juice and a hardy fart. If you like my blog, please consider donating.


 

Post Meta

Share Post

 

Comments: Write Comment

Be the first to write a comment.


Leave a Comment


All comments are reviewed before publishing. Comments must be related to the page topic, must not be spam, and must comply with the criminal code of Canada.