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

keyPressed event: upper and lower case #536

Closed
shiffman opened this issue Feb 12, 2015 · 5 comments
Closed

keyPressed event: upper and lower case #536

shiffman opened this issue Feb 12, 2015 · 5 comments

Comments

@shiffman
Copy link
Member

I'm noticing that in keyPressed() I always get a capital letter for any key pressed. For example with both 'a' and 'A' I get the following:

key code: 65
key: 'A'
function keyPressed() {
  console.log("p5 keyCode: " + keyCode);
  console.log("p5 key: " + key);
}

With native browser events, however

window.addEventListener("keypress", function(e) {
  console.log("native keycode: " + e.which);
  console.log("native key: " + String.fromCharCode(e.which));
});

I get:

key code: 97
key: 'a'

and

key code: 65
key: 'A'

Is this on purpose for a reason I'm missing?

@lmccart
Copy link
Member

lmccart commented Feb 12, 2015

it has to do with differences between browsers... we are updating the keyCode and key on keyPressed, keyReleased, and keyTyped which actually returns different results. this page explains a little more:
http://www.quirksmode.org/js/keys.html

however, this may need to be revisited, I don't know if the discrepancies are still there with the most recent browser versions, or maybe there is something smarter we can do, keeping track of some internal vars and only using the results from the keyup call if the key isn't already set... or maybe key and keyCode just shouldn't be updated at all on keyup?

feel free to play around, it's all in src/input/keyboard, or I'll take a closer look next chance I get.

@ipeluffo
Copy link

Hi!
Feel free to take a look to the following example I've made:
http://jsbin.com/yotobu/6/edit?html,js,console,output

There you'll be able to see that the p5's "keyTyped" event behaves the same as the native event "keypress", and the p5's "keyPressed" event behaves as "keydown" native event.

So, I believe that the behaviour noticed by @shiffman is the normal one :)

@iamutkarshtiwari
Copy link
Contributor

I would like to work on this fix. I had already submitted the patch for a similar issue in PDE -> processing/processing#4006

@lmccart
Copy link
Member

lmccart commented Jan 22, 2016

as @ipeluffo mentions, the behavior noticed is correct. the keypress event in browser events is mapped to keyTyped() in p5, because the keydown event is mapped to keyPressed(). so the best level of accuracy we can get inside keyPressed() is capitalized letters, but if you use keyTyped() you will get the correct casing.

@lmccart lmccart closed this as completed Jan 22, 2016
@SanDiego14
Copy link

e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants