We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › keyPressed listener seems inconsistent
Page Index Toggle Pages: 1
keyPressed listener seems inconsistent (Read 891 times)
keyPressed listener seems inconsistent
Sep 21st, 2007, 6:40pm
 
Hi there,

I'm taking a graduate programming course that's using Processing to explore art, design, and programming. I'm trying to get a little pseudo-game working. However, it became especially apparent when I uploaded the applet, that the keyPressed listener sometimes doesn't work at all.

My code is at http://www.sfu.ca/~mna32/iat800/proj2/

The symptom is that sometimes the commands (w,a,s,d) work just fine. Once they work, they keep working for however long I've tried. However, sometimes the keys don't register (as determined by printing the key in the listener) and when that happens, they never start again until you rerun the program, or -- it seems, the mouse listener registers successfully (I have a reset command).

So this isn't about the key listener working only sometimes (What I'd expect if it were starved by the main loop or something), it's about it not registering at all sometimes.

Now I set up a blank file that just sets a background colour, and it listens to the keyboard just fine.

So, any suggestions (the code is at the website I linked to, naturally)?

Thanks,
-Michael
Re: keyPressed listener seems inconsistent
Reply #1 - Sep 21st, 2007, 6:56pm
 
Hi,

I've tested it, but found no problem with the key listener...

Don't forget that in order for Processing to listen to key events the window must have the focus so you must click once at the applet in order to use the keys...

jorge
Re: keyPressed listener seems inconsistent
Reply #2 - Sep 21st, 2007, 7:13pm
 
Thanks for taking a look at it...

However, I'm testing it by running it from processing and ensuring it has focus. Approximately half the time, my first keypress registers and it's all ok. The other half, it sinks to the bottom and never registers a single keypress. I gather that because when I add the line 'print(key)' it doesn't print anything on these occasions.

I do believe something more than focus is the problem here, although I'll likely change it from a mouse press to 'q' to reset the plane's position to make it easier to ensure focus.

-Michael
Re: keyPressed listener seems inconsistent
Reply #3 - Sep 21st, 2007, 7:18pm
 
I get the same result. Well it works, but only if you click the applet TWICE. I'm not sure if this is a bug in fireforx (probably came with the recent update 2.0.07?).
I've looked at your code, it should work as it is. Even the processing keyboard examples don't work correctly, so I assume it's a browser thing. The applet gets the mouse events from the first click, but keyboard needs two.
In Safari everything works as it should. What browser&version are you using?

Re: keyPressed listener seems inconsistent
Reply #4 - Sep 21st, 2007, 7:43pm
 
Hi there,

Thanks for the insight. Maybe it is "just" a focus issue.

Safari seems to require one click (I'm on a MBP) and maybe you're right about Firefox requiring two.

I guess using mouse press for something as dramatic as reseting the position doesn't work when you need to be clicking to get focus.

The part with it not working on my machine is just weird. I guess sometimes the running applet gets focus all by itself and sometimes it doesn't.

Thanks!
-Michael
Page Index Toggle Pages: 1