KeyUp vs KeyDown
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
KeyUp vs KeyDown
I am trying to make a simple code editor with syntax highlighting.
If i use on keyDown it's working but requires a lot more code. Why keyUp is causing this misbehavior ?
on keyUp pKey
-- in case of empty pKey
if pKey is empty then
exit keyUp
end if
if pKey is a number then
set the textcolor of the last char of field "Field" to "#AE81FF" --violet
else if last word of field "Field" is among the words of "and as assert break class continue def del elif else except finally for" then
set the textcolor of last word of field "Field" to "#F92672"
else if pKey is among the characters of "!=+" then
set the textcolor of the last char of field "Field" to red
else if last word of field "Field" begins with quote and last word of field "Field" ends with quote then
set the textcolor of the last word of field "Field" to yellow
else
set the textcolor of the last char of field "Field" to white
end if
end keyUp
If i use on keyDown it's working but requires a lot more code. Why keyUp is causing this misbehavior ?
on keyUp pKey
-- in case of empty pKey
if pKey is empty then
exit keyUp
end if
if pKey is a number then
set the textcolor of the last char of field "Field" to "#AE81FF" --violet
else if last word of field "Field" is among the words of "and as assert break class continue def del elif else except finally for" then
set the textcolor of last word of field "Field" to "#F92672"
else if pKey is among the characters of "!=+" then
set the textcolor of the last char of field "Field" to red
else if last word of field "Field" begins with quote and last word of field "Field" ends with quote then
set the textcolor of the last word of field "Field" to yellow
else
set the textcolor of the last char of field "Field" to white
end if
end keyUp
Re: KeyUp vs KeyDown
Hi ivelink,
1. welcome to the forum!
2.
Best
Klaus
1. welcome to the forum!

2.
WHAT misbehavior is caused by "KeyUp"?ivelink wrote:Why keyUp is causing this misbehavior ?

Best
Klaus
Re: KeyUp vs KeyDown
I am with Klaus.
The "keyUp" handler works fine. Did you intend to "show" the white chars as you type? Or would it be better to keep the cursor in place, and have a valid word appear all at once? I do see that the white chars are a form of feedback.
Craig
The "keyUp" handler works fine. Did you intend to "show" the white chars as you type? Or would it be better to keep the cursor in place, and have a valid word appear all at once? I do see that the white chars are a form of feedback.
Craig
Re: KeyUp vs KeyDown
Hello Craig,
yes i would like to show them as I type.
Thanks
yes i would like to show them as I type.
Thanks
Re: KeyUp vs KeyDown
I just tested the same script on Mac and it works fine.
Why doesn't work the same on my PC ?
Why doesn't work the same on my PC ?
Re: KeyUp vs KeyDown
PC? What is that?
But if you really can show that the same stack exhibits different behavior between platforms, do submit it to the team.
Craig
But if you really can show that the same stack exhibits different behavior between platforms, do submit it to the team.
Craig
-
- Livecode Opensource Backer
- Posts: 10100
- Joined: Fri Feb 19, 2010 10:17 am
Re: KeyUp vs KeyDown
Is there a difference in performance or just for clarity ?
Re: KeyUp vs KeyDown
If we are talking about "Switch" control structures as opposed to "if/then" control structures, then there is no "correct" situation for either.
That said, I use "if/then" for smaller tasks, and "switch" for larger ones.
That said, "switch" is generally more readable and easier to manage. It has fewer options pertaining how properly to "close" the structure, and is more compartmentalized. One might say it is more "modern".
Craig
That said, I use "if/then" for smaller tasks, and "switch" for larger ones.
That said, "switch" is generally more readable and easier to manage. It has fewer options pertaining how properly to "close" the structure, and is more compartmentalized. One might say it is more "modern".
Craig
Re: KeyUp vs KeyDown
Correct me if I'm wrong, but I think "switch" can be somehow faster if you sort the cases from the most probable to the rarest, because it'll jump straight to the "end switch" statement as soon as a case is met, without reading the following cases. With "ifs" the engine has to read all the code every time. I think this is why Craig said it's good for larger tasks.
Re: KeyUp vs KeyDown
Thanks.
That makes sense.
I will try. Hopefully will resolve the issue too.
That makes sense.
I will try. Hopefully will resolve the issue too.
Re: KeyUp vs KeyDown
An "if" structure will behave just like a switch structure provided all ifs after the first one begin with "else if". But if each "if" is written independently then it acts as you describe.FredBeck wrote:Correct me if I'm wrong, but I think "switch" can be somehow faster if you sort the cases from the most probable to the rarest, because it'll jump straight to the "end switch" statement as soon as a case is met, without reading the following cases. With "ifs" the engine has to read all the code every time. I think this is why Craig said it's good for larger tasks.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: KeyUp vs KeyDown
What Jacque said. Switch works similarly, with the "case" (if any) that resolves to "true" being accessed directly. In a button somewhere:
Step through. The number in it will always be 2 or more (so that "1" is never chosen, which might be misleading), and you can see that the proper case is chosen immediately.
Craig
Code: Select all
on mouseUp
get random(8) + 1
breakpoint
switch it
case 1
break
case 2
break
case 3
break
case 4
break
case 5
break
case 6
break
case 7
break
case 8
break
case 9
break
end switch
end mouseUp
Craig