Commit 2978043d authored by loelkes's avatar loelkes
Browse files

add KeyMapping() class

parent c38fb3b9
......@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [1.2.0] - 2019-01-15
### Added
- Class KeyMapping() for hadling key events such as keydown.
## [1.1.0] - 2019-01-13
### Added
......
'use strict';
console.debug('zkmtools v1.1.0');
console.debug('zkmtools v1.2.0');
export function blink( target, interval = 50, duration = 500 ) {
var blinkTimer = setInterval( function() {
......@@ -102,3 +102,36 @@ export default class FullscreenUtils {
}
}
}
export class KeyMapping {
constructor( eventType='keydown' ) {
this.callbacks = {};
document.addEventListener( eventType, this.handleEvent.bind( this ) );
}
addCallback( keyName, callback ) {
this.callbacks[ keyName ] = callback;
}
removeCallback( keyName ) {
delete this.callbacks[ keyName ];
}
runCallback( keyName ) {
if( keyName in this.callbacks ) {
return this.callbacks[ keyName ]();
}
return false;
}
handleEvent( event ) {
const keyName = event.key;
if( keyName === 'Control' ) return;
if( event.ctrlKey ) {
console.debug( 'Combination of ctrlKey + %s', keyName);
} else {
console.debug( 'Key pressed: %s', keyName );
this.runCallback( keyName );
}
}
}
{
"name": "zkmtools",
"version": "1.1.0",
"version": "1.2.0",
"description": "Useful tools for building javascript things",
"main": "index.js",
"repository": {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment