Keydown Help..!
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Keydown Help..!
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9842
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Keydown Help..!
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
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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Livecode Opensource Backer
- Posts: 9388
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Keydown Help..!
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!
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!
Re: Keydown Help..!
Is there a way to get the keyDown function to only detect one press before resetting?
Right now I have:
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?
Right now I have:
Code: Select all
on keyDown spacebar
add 1 to the field "Score"
end keyDown
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Keydown Help..!
Hi.
I don't suppose simply turning off the key repeat would do?
Anyway, try this in the card script:
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
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
Craig Newman
Re: Keydown Help..!
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:dunbarx wrote:Why do we not need to identify any key at all in the "rawKeyUp" handler?
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
-
- VIP Livecode Opensource Backer
- Posts: 9670
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Keydown Help..!
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
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..!
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..
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.
Of course, you can use rawkeydown for all your key pressing needs
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
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
tobx wrote: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:dunbarx wrote:Why do we not need to identify any key at all in the "rawKeyUp" handler?This is exactly what I was looking for! Thanks so much for your help again, Craig!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