### Install keycharm via npm Source: https://github.com/alexdm0/keycharm/blob/master/README.md Install the keycharm library from npm. ```bash npm install keycharm ``` -------------------------------- ### Keycharm event binding example Source: https://github.com/alexdm0/keycharm/blob/master/test/test.html Initializes keycharm with a container and preventDefault false, binds all keydown and keyup events to update the display, and binds/unbinds a specific key 'a' to an alert function. ```javascript var keys = keycharm({container:document.getElementById("container"),preventDefault: false}); keys.bindAll(function(event) { document.getElementById('keydown').innerHTML = "Key:" + keys.getKey(event); }, 'keydown'); keys.bindAll(function(event) { document.getElementById('keyup').innerHTML = "Key:" + keys.getKey(event); }, 'keyup'); var alertFunction = function() {alert("a!");}; keys.bind("a", alertFunction); keys.unbind("a", alertFunction); ``` -------------------------------- ### CSS styles for keycharm example Source: https://github.com/alexdm0/keycharm/blob/master/test/test.html CSS styles for the keyfield display and the container background. The container needs a tabindex to receive keyboard events. ```css .keyfield { display:inline-block; width:190px; height:25px; background-color:#eeeeee; vertical-align:middle; } #container { background-color:#dddddd; } ``` -------------------------------- ### Create keycharm instance and bind a key Source: https://github.com/alexdm0/keycharm/blob/master/README.md Create a keycharm instance and bind a key. The bind method takes a key, a callback, and an optional event type ('keydown' or 'keyup'). ```javascript var keys = keycharm(options); keys.bind("a", function () {}, 'keydown'); // key, callback function, 'keydown' or 'keyup' ``` -------------------------------- ### Supported keys list Source: https://github.com/alexdm0/keycharm/blob/master/README.md List of supported keys for keycharm bindings. ```text 'a'-'z', 'A'-'Z', '0'-'9', 'space', 'enter', 'ctrl', 'alt', 'tab', 'shift', 'delete', 'backspace', '-', '=', '[', ']', 'esc', 'F1'-'F12', 'pageup', 'pagedown', 'left', 'up', 'right', 'down', 'num0'-'num9', 'num/', 'num*', 'num-', 'num+', 'num.' ``` -------------------------------- ### Import keycharm with CommonJS Source: https://github.com/alexdm0/keycharm/blob/master/README.md Import keycharm using CommonJS require. ```javascript const keycharm = require('keycharm'); ``` -------------------------------- ### Import keycharm with ESM Source: https://github.com/alexdm0/keycharm/blob/master/README.md Import keycharm using ES module syntax. ```javascript import keycharm from 'keycharm'; ``` -------------------------------- ### keycharm Methods Source: https://github.com/alexdm0/keycharm/blob/master/README.md Methods available on the keycharm instance for binding and managing keyboard events. ```APIDOC ## [bind] keycharm.bind(key, callback, [type]) ### Description Binds a callback function to a key event. The type parameter specifies whether to bind to 'keydown' or 'keyup', defaulting to 'keydown'. ### Method bind ### Parameters - **key** (string) - Required - The key to bind, e.g., 'a', 'space', 'ctrl'. - **callback** (function) - Required - The function to execute when the key event occurs. - **type** (string) - Optional - The event type: 'keydown' or 'keyup'. Defaults to 'keydown'. ### Request Example ```js keys.bind("a", function () {}, 'keydown'); ``` ### Response No return value. ``` ```APIDOC ## [unbind] keycharm.unbind(key, [callback], [type]) ### Description Unbinds a callback function from a key event. If no callback is provided, all bound callbacks for the key are removed. The type parameter specifies 'keydown' or 'keyup', defaulting to 'keydown'. ### Method unbind ### Parameters - **key** (string) - Required - The key to unbind. - **callback** (function) - Optional - The specific callback to remove. If omitted, all callbacks for the key are removed. - **type** (string) - Optional - The event type: 'keydown' or 'keyup'. Defaults to 'keydown'. ### Request Example ```js keys.unbind("a"); ``` ### Response No return value. ``` ```APIDOC ## [reset] keycharm.reset() ### Description Removes all bound keys from the keycharm instance. ### Method reset ### Parameters None. ### Request Example ```js keys.reset(); ``` ### Response No return value. ``` ```APIDOC ## [destroy] keycharm.destroy() ### Description Removes all bound keys and the event listeners of keycharm. ### Method destroy ### Parameters None. ### Request Example ```js keys.destroy(); ``` ### Response No return value. ``` ```APIDOC ## [getKey] keycharm.getKey(event) ### Description Gets the key label of the given event. ### Method getKey ### Parameters - **event** (Event) - Required - The event object from which to extract the key label. ### Request Example ```js keys.getKey(event); ``` ### Response Returns the key label as a string. ``` ```APIDOC ## [bindAll] keycharm.bindAll(function, type) ### Description Binds all keys to the given function. This can be used for testing or demos. ### Method bindAll ### Parameters - **function** (function) - Required - The function to bind to all keys. - **type** (string) - Required - The event type: 'keydown' or 'keyup'. ### Request Example ```js keys.bindAll(function() {}, 'keydown'); ``` ### Response No return value. ``` -------------------------------- ### Import keycharm via IIFE script tag Source: https://github.com/alexdm0/keycharm/blob/master/README.md Load keycharm in the browser via a script tag, making it available globally. ```html