Giter Site home page Giter Site logo

How to prevent keyboard long pressed? about gkm HOT 4 CLOSED

tomzx avatar tomzx commented on June 26, 2024
How to prevent keyboard long pressed?

from gkm.

Comments (4)

tomzx avatar tomzx commented on June 26, 2024 1

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.

tomzx avatar tomzx commented on June 26, 2024

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.

futurouz avatar futurouz commented on June 26, 2024

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

futurouz avatar futurouz commented on June 26, 2024

@tomzx That's work. Thanks!

from gkm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.