In a text field script, I am trying to capture the keystroke so that I can exit the field when the tab key or enter key is used. I want to know if the user enters a "Tab", "Enter", UPPERCASE or lowercase letter, the space bar, the apostrophe ('), or a digit. I have tried using "on rawKeyUp", "on rawKeyDown", "on tabKey", "on enterKey". Some of these work, others do not.
I have gone "round and round" so much so that my code needs to start fresh.
Anybody have some code that works?
Thank you
Identifying user keystroke
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- VIP Livecode Opensource Backer
- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Identifying user keystroke
RawKeyDown is generally useful for keys that trigger actions, but for modifier keys (Shift, Caps Lock, etc.) you may want to also check the comma-delimited list of values returned from the keysDown function.
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
Re: Identifying user keystroke
Ok, ino order... If the tabkey is pressed and you don't have an "on tabkey" handler interfering, the cursor should traverse to the next field on its own. If however, you only have 1 field and you wish to have it lose focus when you press tab, you do need to "capture" it.
So, in your field you could put this (I think, untested)
on tabkey
focus on nothing
end tabkey
Don't "pass tabkey" or you'll end up focusing back on the field.
K next. If you have autotab turned on, pressing return will behave as a tab key and again send you to the next field. You can use: on returninfield to catch and change the behavior. Also enterinfield.
If you want to use keydown to trap character key presses but need to know case, not sure of an easy way to do that (probably is one, just is a bridge i've never needed to cross)
Also, as FourthWorld said, checking keysdown() (polling) will probably be useful too. There are some minor quirks with keysdown, specifically there are a few keyboards (think most if not all of the quirking ones being laptop keyboards) where multiple arrow key presses in some combinations just don't work. You get the first key pressed, not the second. (if it was right, and left it might be understandable, but I think the last keyboard I had that did that it was perpendicular arrows that wouldn't read together) Been a while though so not sure, and think its a keyboard issue not an lc issue.
rawkeydown would be better.
if you put:
on rawkeydown pKey
put pKey
pass rawkeydown
end rawkeydown
You will get the keycode for the key that was pressed. So A is 65, a is 97 as expected. Without knowing exactly where you're headed with this, not sure how specific I can be as far as getting the job done, but hopefully theres enough info here to get you on the track.
So, in your field you could put this (I think, untested)
on tabkey
focus on nothing
end tabkey
Don't "pass tabkey" or you'll end up focusing back on the field.
K next. If you have autotab turned on, pressing return will behave as a tab key and again send you to the next field. You can use: on returninfield to catch and change the behavior. Also enterinfield.
If you want to use keydown to trap character key presses but need to know case, not sure of an easy way to do that (probably is one, just is a bridge i've never needed to cross)
Also, as FourthWorld said, checking keysdown() (polling) will probably be useful too. There are some minor quirks with keysdown, specifically there are a few keyboards (think most if not all of the quirking ones being laptop keyboards) where multiple arrow key presses in some combinations just don't work. You get the first key pressed, not the second. (if it was right, and left it might be understandable, but I think the last keyboard I had that did that it was perpendicular arrows that wouldn't read together) Been a while though so not sure, and think its a keyboard issue not an lc issue.
rawkeydown would be better.
if you put:
on rawkeydown pKey
put pKey
pass rawkeydown
end rawkeydown
You will get the keycode for the key that was pressed. So A is 65, a is 97 as expected. Without knowing exactly where you're headed with this, not sure how specific I can be as far as getting the job done, but hopefully theres enough info here to get you on the track.
Re: Identifying user keystroke
Learning poses its hazards!!
After trying all of the "fancy" stuff, the solution was easy!
I found another post that discussed using closeField and exitField. These are the tools that I was looking for.
closeField will "act" if the user changes the field and leaves it.
exitField will "act" if the user does no changes and leaves it.
Also, I had to set the "autoTab" property of the text field so that a "return" will act as a "tab".
After trying all of the "fancy" stuff, the solution was easy!
I found another post that discussed using closeField and exitField. These are the tools that I was looking for.
closeField will "act" if the user changes the field and leaves it.
exitField will "act" if the user does no changes and leaves it.
Also, I had to set the "autoTab" property of the text field so that a "return" will act as a "tab".
Re: Identifying user keystroke
There is also "textchanged" that might be useful. Thought you were trying to capture and perform an action based on what key was pressed rather than just look for a change, glad you figured out a method!
If you are only checking for user actions that change the text, and if you want to do it "live" as things change, textchanged works great
on textchanged
--do whatever you need to do here when the user causes a change in the field text.
end textchanged
If you are only checking for user actions that change the text, and if you want to do it "live" as things change, textchanged works great
on textchanged
--do whatever you need to do here when the user causes a change in the field text.
end textchanged