Page 1 of 1

Blocking a key for most uses

Posted: Wed Apr 02, 2008 9:58 am
by gjn1w07
I'm writing an application where there are effectively two tasks going on. One task involves writing answers to questions in various boxes. At the same time another task involves listening to numbers being read out and pressing a key when 3 odd numbers are heard in a row.

My question is can I block out one key so that it ONLY, say, puts a 1 in a variable and does nothing else? What I'm aiming for here is a key that doesn't interfere with the person's responding to the questions when they press it. For instance if it were the space bar I was to use, I wouldn't want lots of random spaces being inserted into their answers.

Posted: Wed Apr 02, 2008 10:15 am
by Mark
Dear gjn1w07,

How doesn't this answer your question?

http://forums.runrev.com/phpBB2/viewtop ... ight=#6331

Best regards,

Mark

Posted: Wed Apr 02, 2008 11:22 am
by gjn1w07
That was a slightly different question, hence the seperate thread. That did answer my original question, which was just "how do I use the space bar in the code"

This question is how do I for instance, have the "g" key ONLY put 1 into the variable tGPressed, and not pass the fact that it has been pressed to any other field? So for instance if the user was in a text entry box typing in the word "hello" and pressed the g key in the middle of typing, I want the g key press to put 1 into the variable tGPressed without actually typing a g. So if a user pressed g in the middle of typing hello, it ends up as "hello" and not "helglo". In effect I want to disable the key for all but one use for the card.

Posted: Wed Apr 02, 2008 11:30 am
by Mark
Dear gjn1w07,

The script is still the same as in the other thread:

Code: Select all

on keyUp theKey
  if theKey = <some character> then
    --do stuff here
  end if
end keyUp
If you want to take several different keys into account, just add some if-then statements:

Code: Select all

on keyUp theKey
  if theKey = <some character> then
    --do stuff here
  if theKey = <some other character> then
    --do other stuff here
  else
    pass keyUp
  end if
end keyUp
Make sure to put any characters in quotes.

Kindest regards,

Mark

Posted: Wed Apr 02, 2008 10:59 pm
by BvG
To interrupt keys, you need the keydown message, without passing it. Look at these documentation entries:

keyDown message
keyUp message
pass control structure

Posted: Thu Apr 03, 2008 11:16 am
by gjn1w07
Thanks for all that. I now see how this is working. Cheers all!

Posted: Thu Apr 03, 2008 9:14 pm
by Garrett
Would rawKeyDown and rawKeyUp be better to use?

Posted: Thu Apr 03, 2008 9:26 pm
by Mark
Hi Garrett,

If you know that you want to "catch" a specific character, the space bar or e.g. a question mark, keyUp and keyDown are sufficient. RawKeyUp and rawKeyDown are slightly more complex and useful if you want a script to respond to the backspace key, the delete key, escape key, et cetera. Nonetheless it is possible to use rawKeyUp instead of rawKeyDown if you need to.

Btw, BvG is right about keyDown, of course, if you don't want the "g" to be put into the field you're typing in. I wonder how one would write "good morning" though ;-)

Best,

Mark

Posted: Thu Apr 03, 2008 9:32 pm
by BvG
They work differently. The rawKey messages use numbers, the same ones as the keysdown() function. The key messages use the chars themselves.

This means that all characters that can't be typed into the script editor as a string, will not be usable with the key messages. A good example is the return key, you can't just type that into a string in the script editor, therefore it only works with the rawKey messages.

Posted: Fri Apr 04, 2008 8:29 am
by Garrett
Ahhh.. got ya. Thanks. :-)