Comments (4)
You probably want to trigger the first time key.pressed
is called, have a small object which contains which keys are currently pressed (something like {'a': true, 'c': true}
, and on key.released
remove the key from the list. This way, you check if the key is currently in this object, and if it is, then you ignore the key press.
This way you can trigger an event on the initial key.pressed
, ignore subsequent events, then do something else on key.released
(if necessary).
const pressedKeys = {};
gkm.events.on('key.pressed', function(data) {
if (pressedKeys[data]) {
// Ignore
return;
}
pressedKeys[data] = true;
console.log(this.event + ' ' + data);
});
gkm.events.on('key.released', function(data) {
delete pressedKeys[data];
console.log(this.event + ' ' + data);
});
```
from gkm.
Could you explain your use case in more details?
Normally if you subscribe to key.pressed
and key.released
, you will receive only 1 event when the key is pressed and one once it's released, such that a "long" or "short" key press/release are handled the same way.
from gkm.
@tomzx I'm try to count keystroke. gkm work fine but when I try long keypressed the number of couter increment so fast.
let counter = 0; gkm.event.on('key.pressed',() => { ++counter console.log(counter) })
I suppose when I long keypressed the counter. It's should increment counter only 1 not continue increment.
from gkm.
@tomzx That's work. Thanks!
from gkm.
Related Issues (20)
- Allow consuming events
- After reinstall java, GKM not working on my project?
- How to delete listeners? HOT 1
- Publish the latest v0.3.0 to NPM HOT 1
- key.typed event returns [ 'Undefined' ] HOT 1
- Does not work on linux HOT 1
- Gkm with ReactJS not working HOT 3
- Test on MacOS HOT 5
- not working with Electron-packager HOT 5
- How is it possible to get mouse.wheel events? HOT 1
- `key.released` event doesn't always seem to fire when a key is released HOT 1
- JNativeHook.jar does not work with jdk15.0.2 64-bit (workaround available) HOT 2
- EventEmitter2 is not a constructor HOT 1
- Is there any way to prevent the released event from firing? HOT 1
- gkm.jar source code HOT 1
- What is inside gkm.jar? HOT 1
- Is there a way to get the keyboard cursor location? HOT 1
- Mouse Wheel HOT 2
- angular2 not supporting yet HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gkm.