Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to prevent keyboard long pressed? #16

Closed
oukunan opened this issue May 10, 2018 · 4 comments
Closed

How to prevent keyboard long pressed? #16

oukunan opened this issue May 10, 2018 · 4 comments

Comments

@oukunan
Copy link

oukunan commented May 10, 2018

Is it possible to prevent that?

@tomzx
Copy link
Owner

tomzx commented May 10, 2018

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.

@oukunan
Copy link
Author

oukunan commented May 10, 2018

@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.

@tomzx
Copy link
Owner

tomzx commented May 10, 2018

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);
});
``

@oukunan
Copy link
Author

oukunan commented May 11, 2018

@tomzx That's work. Thanks!

@tomzx tomzx closed this as completed May 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants