Keydown Help..!

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
snop21
Posts: 90
Joined: Mon Jan 20, 2014 2:54 pm

Keydown Help..!

Post by snop21 » Mon Feb 03, 2014 10:39 am

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7647
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Keydown Help..!

Post by FourthWorld » Mon Feb 03, 2014 3:04 pm

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
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 4353
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Keydown Help..!

Post by richmond62 » Tue Feb 04, 2014 11:44 am

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

tobx
Posts: 16
Joined: Thu Jun 04, 2015 5:17 pm

Re: Keydown Help..!

Post by tobx » Sun Jun 07, 2015 7:56 pm

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?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6617
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Keydown Help..!

Post by dunbarx » Sun Jun 07, 2015 10:29 pm

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

tobx
Posts: 16
Joined: Thu Jun 04, 2015 5:17 pm

Re: Keydown Help..!

Post by tobx » Mon Jun 08, 2015 8:28 pm

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!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6617
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Keydown Help..!

Post by dunbarx » Mon Jun 08, 2015 10:20 pm

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1682
Joined: Sat Feb 28, 2009 11:49 pm

Re: Keydown Help..!

Post by sturgis » Mon Jun 22, 2015 1:09 am

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!

Post Reply

Return to “Mac OS”