Skip to content

Fix keyPressed for multiple keys#5050

Merged
benfry merged 1 commit into
processing:masterfrom
JakubValtar:fix-keypressed
May 5, 2017
Merged

Fix keyPressed for multiple keys#5050
benfry merged 1 commit into
processing:masterfrom
JakubValtar:fix-keypressed

Conversation

@JakubValtar

Copy link
Copy Markdown
Contributor

Java2D and FX2D send multiple PRESSED and only one RELEASE event (at
least on Windows). Therefore we have to keep track of what is pressed
and what not. Most keyboards do not support pressing more than ~10 keys
simultaneously, so this should not cause any performance problems.

See focusLost() for possible problem when user overrides it and does not call super. It's there only to clear keys when ALT+TAB is pressed so the chance somebody will hit this is low.

Fixes #5049

Java2D and FX2D send multiple PRESSED and only one RELEASE event (at
least on Windows). Therefore we have to keep track of what is pressed
and what not. Most keyboards do not support pressing more than ~10 keys
simultaneously, so this should not cause any performance problems.

Fixes processing#5049
@benfry benfry merged commit a22af42 into processing:master May 5, 2017
@JakubValtar JakubValtar deleted the fix-keypressed branch May 5, 2017 13:18
@GoToLoop

GoToLoop commented Jun 4, 2017

Copy link
Copy Markdown

Perhaps a HashSet<Long> would be a better fit than an ArrayList<Long> for field pressedKeys here:
http://docs.Oracle.com/javase/8/docs/api/java/util/HashSet.html

Using a Set instead, statement if (!pressedKeys.contains(hash)) pressedKeys.add(hash);
can be replaced by pressedKeys.add(hash); only; given a Set can't have duplicated elements after all.

Due to its hashable searching nature, a Set should be more performant when using methods such as contains(Object) & remove(Object) than a List.

@GoToLoop

GoToLoop commented Jun 4, 2017

Copy link
Copy Markdown

On the other hand, if the # of elements in the container pressedKeys is always small, I guess a List might be indeed faster than a Set in this case. O_o

@github-actions

Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jun 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

problem when pressing and holding key

3 participants