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
keyIsDown function #411
keyIsDown function #411
Conversation
I like this approach. I keep track of key pressed using keyPressed()/keyReleased(). As an addendum what about a sketch variable like keys[]? |
@zaerl Glad you like it! About keys[], it sort of already exists as the private variable p5.prototype._downKeys, I guess it could be exposed if need be. But what would be the purpose? IMO it looks cleaner like this: if (keyIsDown(LEFT_ARROW)) {
// do something.
} Than like this: if (keys.indexOf(LEFT_ARROW)>=0) {
// do something.
} In order to check if a key is pressed. But maybe there are other use cases that would be solved by exposing keys[]? If so, which for example? |
I see a keys[] variable as a supplement to the keyIsDown call, not a substitute. If you expose all the keys you can iterate them during a draw call for example. |
Ok I see... Makes sense... Anyone else who ha a chance to look at it? |
I have tested this out on the firefox version(32.0) am using and it seems to work fine. |
@limikael, what you're proposing already exists across Processing frameworks. |
Unfortunately keyCode contains the last keyboard key pressed, you can not keep track of more than one key pressed at the same time. You can only track them with keyPressed()/keyReleased(). |
@GoToLoop, yes as @zaerl says, it is only possible to track one key at a time using that method. Check this example: https://github.com/limikael/p5.js/blob/master/examples/keyboard/gamestyle.js And notice that it is possible to move the object diagonally by pressing two arrow keys at the same time. This is something you would expect if you are creating a game. |
That's the "traditional" way of keeping track of multiple keys across Processing frameworks! 👐 |
Indeed it's much easier that way. Although it's delegated to the framework the task to keep track which keys are currently pressed! Dunno how much that would slow the framework. Perhaps nothing much!? |
And remember that Processing isn't exactly a game framework, but for graphics artists! 😎 |
I think that this is a nice addition that does not slow down the entire thing 😄 |
Although that'd abstract away the formal keyPressed()/keyReleased() way of doing that, it's undeniable how much more intuitive & easier & lazier it gets! 😜 |
Thanks for the comments!
Maintaining the list of currently pressed keys is done by keeping them in an array, which rarely contains more than 2 entries, so I would argue nothing much.
As far as I understand, it is a framework for graphics artists who wants to learn programming. Wouldn't it then be in their interest to show a welcoming attitude towards other programmers? If the community around p5.js would grow to also include programmers who has an interest in game design, then the shared knowledge base of the community would grow, and this would be in everyone's interest I think. |
/** | ||
* The keyIsDown function checkes if the key is currently down, i.e. pressed. | ||
* It can be used if you have an object that moves, and you want several keys | ||
* to be able to affect its behaivour simoultaneously, such as moving a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checkes -> checks. Remove extra 2nd vowel e
...
behaivour -> behaviour or behavior. That is, swap mistyped vowel i
to after consonant v
. ^_^
simoultaneously -> simultaneously. That is, just erase the extra mistyped 1st o
vowel! HEHE
I've got a modernized & radical propose: Instead of a regular We only need 3 of its methods: add(), delete() & has(). And all of those 3 methods are already implemented in all 5 major browsers! So they're safe to use right now! 😺
Take a look at how more concise and easier your keyIsDown() gets after
|
Only thing that still puzzles me is why _downKeys is a property of prototype. |
I haven't been following carefully, so I might have missed something, but why an Array or a Set, and not just an object? Is it slower? It looks more readable to me:
@GoToLoop Isn't it the other way around? Without prototype it would be a static, with prototype each p5 object has it's own set of _downKeys. Or I'm too sleepy :) |
In Java, objects contain instance variable fields only. Rest of the members stay in their original class! |
It'd be a complete waste of RAM if methods/functions weren't P.S.: I still prefer |
I think that explaining how javascript prototypal inheritance model works is far beyond the scope of this discussion. Please just close this issue and merge the commit if you think that it's a good addition to the API. |
Ok fixed some spelling mistakes... It would make sense to use What about the original questions?
|
That's true for the time being, but as I've mentioned already, But for the time being, just to give some more months for Chrome/Chromium version 38 to be more wide-spread, I'd go w/ @hamoid's idea of
It is too! We've already got keyIsPressed: http://p5js.org/reference/#/p5/keyIsPressed |
hi @zaerl and all, I will review and merge if it makes sense next week after the holiday |
thanks everyone for the thoughtful discussion. @limikael one question, does I think the name sounds good, but would |
You are right @lmccart, there is no reason for _downKeys to be part of the prototype. I was going to change it and create a new pull request but saw that you had already changed it... :) Hm, I don't know if keyDown would make more sense actually... It is true that names with "Is" in is only used for variable names currently, but it is only those two, so I don't know if that actually means that it feels like a "convention" for the users of p5.js. It is not really a convention in other libraries either, the closest that comes to mind is java: http://www.cwu.edu/~gellenbe/javastyle/method.html but it says nothing about "is" being reserved specifically for variables. So I think keyIsDown sounds better than keyDown, also because it differentiates the function from keyPressed which is a callback. So maybe keyIsDown is the best actually... |
yes I was thinking about this again last night and I agree completely with your name reasoning, let's leave as is. thank you for adding this functionality to the library!! sorry it took so long to get it all merged in. |
Ok! Thank you for giving me the opportunity to participate! :)
|
I took a look at this issue a while back and I always had problems with the tab keys forcing all of the other keys to lock as being "down" after the focus was lost on the page while the keys were being pressed. So I wrote up a little fiddle that solved it. I'm not sure if you guys took this issue into consideration or already accounted for it. but heres the fiddle: http://jsfiddle.net/Darbicus/jfh8jgy8/ |
Good ideas to empty the container on blur, and to store the key press It's something minor, as people can not press the keys thousands of Then, to test if the key is down one can check if the value is not 0. |
Dunno whether you've noticed it, but internal implementation changed from |
@darbicus, it makes perfect sense and it is not something we have accounted for so far... it would be awesome to have that added, in my opinion... have you thought about adding it and creating i pull request? |
@hamoid yeah that's a good idea. I'm not sure if it is in p5.js because I didn't find anything like that in the code. I only saw a random event Listener for 'focus' and 'blur' that set the property focused to true or false. I would add the functionality right now, but since you said "optionally." I believe we would have to discuss what the keyword that enabled this feature. animateOnFocus? or pauseAnimationOnBlur? I don't like either because they are too long. Is there a pause animation function? because then users could implement this functionality themselves if they wanted just by adding addEventLIstener('blur',function(){p5.pause();}); I pulled it to your fork limikael because it is your function and I haven't tested it to make sure the blur event even registers. But assuming they will it should work fine. |
yes, this is a great idea! there's nothing like this in there yet. |
Personally I would pause on blur by default... Imagine how many Probably this discussion should move to a new thread... |
Please see this as the start of a discussion rather than something ready to integrate...
Anyway, the idea with this is to be able to check if a key is currently pressed, in order to get a game like behaviour where it is possible to press for two keys and be able to move a sprite diagonally.
check the example in keyboard/gamestyle.js
Two issues that I see currently:
Let me know what you think!