Page 1 of 1

Keydown Help..!

Posted: Mon Feb 03, 2014 10:39 am
by snop21
Hi livecoders,

Will just ask how to use keydown event?

I am trying to do if I am going to press the key Button "ENTER" prompt message "Okay" else if "Esc" will prompt "No"

Thanks Livecoders..!

-snop21

Re: Keydown Help..!

Posted: Mon Feb 03, 2014 3:04 pm
by FourthWorld
Not all keys are handled in the keydown message. LiveCode key messages are often handled in related groups for convenience in most conventional circumstances. To trap all keys from a single handler, see the rawKeyDown message. But in your case, since the Enter and Return keys are so commonly used, LiveCode also provides separate messages for those: enterKey and returnKey,

FWIW, I've submitted a request to the LCQCC to have enterKey and returnKey added to the See Also section of the Dictionary entry for the keyDown message, so hopefully others in the future will be able to find them easier.
http://quality.runrev.com/show_bug.cgi?id=11752

Re: Keydown Help..!

Posted: Tue Feb 04, 2014 11:44 am
by richmond62
If you set up a little stack with a card script like this:

on rawKeyDown RKD
put RKD
end rawKeyDown

and then you will be able to work out what the rawKey codes are for the 2 enter keys (one is also called RETURN)

on Linux the RETURN keys give me 65293

then you can do something of this sort in your work:

on rawKeyDown RKD
switch RKD
case 65293
do something
break
default
pass rawKeyDown
end rawKeyDown

Make sure you include the DEFAULT statement, otherwise you will end up with a useless keyboard!
LC_gadfly.png

Re: Keydown Help..!

Posted: Sun Jun 07, 2015 7:56 pm
by tobx
Is there a way to get the keyDown function to only detect one press before resetting?

Right now I have:

Code: Select all

on keyDown spacebar
   add 1 to the field "Score"
end keyDown
The idea is for the user to earn 1 point every time they press the space bar, but this code allows them to simply hold the space bar and earn points. Any ideas?

Re: Keydown Help..!

Posted: Sun Jun 07, 2015 10:29 pm
by dunbarx
Hi.

I don't suppose simply turning off the key repeat would do?

Anyway, try this in the card script:

Code: Select all

on rawKeyDown tKey
      if the enableAddScore of this card = "true" and tKey is 32 then add 1 to fld "score"
       set the enableAddScore of this card to "false"
end rawKeyDown

on rawKeyUp tKey
       set the enableAddScore of this card to "true"
end rawKeyUp
This is what is known as setting and reading a flag. Your job is to see why it works. Why do we not need to identify any key at all in the "rawKeyUp" handler?

Craig Newman

Re: Keydown Help..!

Posted: Mon Jun 08, 2015 8:28 pm
by tobx
dunbarx wrote:Why do we not need to identify any key at all in the "rawKeyUp" handler?
It looks like pressing the spacebar disables the condition and any key going up (e.g., releasing the spacebar) reactivates it, but the only key that triggers this keyup condition is the button that initiated it - the spacebar. Cool! This solves the repeat but I'm noticing that pressing any button before the spacebar deactivates the flag. I messed around with specifying keys and this ended up working regardless of unwanted keypresses:

Code: Select all

on keyDown spacebar
      if the enableAddScore of this card = "true" then add 1 to fld "Score"
      set the enableAddScore of this card to "false"
end keyDown

on rawKeyUp tKey
   if tKey is 32 then set the enableAddScore of this card to "true"
end rawKeyUp
This is exactly what I was looking for! Thanks so much for your help again, Craig!

Re: Keydown Help..!

Posted: Mon Jun 08, 2015 10:20 pm
by dunbarx
Good to hear.

So you see that if you have a reasonable vocabulary of properties, messages and commands, you can construct a handler to do almost anything you wish. Keep at it...

Craig

Re: Keydown Help..!

Posted: Mon Jun 22, 2015 1:09 am
by sturgis
I notice you're using "spacebar" as the name of the parameter used in the keyDown handler.
All that means is that you now have a local variable named "spacebar" with a numeric keycode value in it.
To see what I mean, you should create a test handler..

Code: Select all

on keyDown spacebar
   put spacebar -- will display the keycodes for each key pressed in the message box
end keyDown
Then start pressing keys. You'll see the keycode for each one pop up. Since (as you use in rawkeydown) the spacebar is keycode 32, you'd want to actually check for that value.

Something like this.. Otherwise things will occur when ANY key (that sends a keydown message) is pressed.

Code: Select all

on keyDown theKey
if theKey is 32 then
       if the enableAddScore of this card = "true" then add 1 to fld "Score"
      set the enableAddScore of this card to "false"
end if
end keyDown
Of course, you can use rawkeydown for all your key pressing needs

tobx wrote:
dunbarx wrote:Why do we not need to identify any key at all in the "rawKeyUp" handler?
It looks like pressing the spacebar disables the condition and any key going up (e.g., releasing the spacebar) reactivates it, but the only key that triggers this keyup condition is the button that initiated it - the spacebar. Cool! This solves the repeat but I'm noticing that pressing any button before the spacebar deactivates the flag. I messed around with specifying keys and this ended up working regardless of unwanted keypresses:

Code: Select all

on keyDown spacebar
      if the enableAddScore of this card = "true" then add 1 to fld "Score"
      set the enableAddScore of this card to "false"
end keyDown

on rawKeyUp tKey
   if tKey is 32 then set the enableAddScore of this card to "true"
end rawKeyUp
This is exactly what I was looking for! Thanks so much for your help again, Craig!