KeyUp vs KeyDown

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ivelink
Posts: 7
Joined: Thu Nov 27, 2014 1:12 pm

KeyUp vs KeyDown

Post by ivelink » Tue Dec 09, 2014 3:53 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: KeyUp vs KeyDown

Post by Klaus » Tue Dec 09, 2014 5:11 pm

Hi ivelink,

1. welcome to the forum! :D

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


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: KeyUp vs KeyDown

Post by dunbarx » Tue Dec 09, 2014 9:06 pm

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

ivelink
Posts: 7
Joined: Thu Nov 27, 2014 1:12 pm

Re: KeyUp vs KeyDown

Post by ivelink » Tue Dec 09, 2014 10:01 pm

Hello Craig,

yes i would like to show them as I type.

Thanks

ivelink
Posts: 7
Joined: Thu Nov 27, 2014 1:12 pm

Re: KeyUp vs KeyDown

Post by ivelink » Wed Dec 10, 2014 12:05 am

I just tested the same script on Mac and it works fine.
Why doesn't work the same on my PC ?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: KeyUp vs KeyDown

Post by dunbarx » Wed Dec 10, 2014 4:25 am

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

ivelink
Posts: 7
Joined: Thu Nov 27, 2014 1:12 pm

Re: KeyUp vs KeyDown

Post by ivelink » Wed Dec 10, 2014 2:15 pm

Here is the stack
Attachments
test.rar
(694 Bytes) Downloaded 211 times

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10100
Joined: Fri Feb 19, 2010 10:17 am

Re: KeyUp vs KeyDown

Post by richmond62 » Wed Dec 10, 2014 9:45 pm

Why not use CASE ?

ivelink
Posts: 7
Joined: Thu Nov 27, 2014 1:12 pm

Re: KeyUp vs KeyDown

Post by ivelink » Thu Dec 11, 2014 1:25 am

Is there a difference in performance or just for clarity ?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: KeyUp vs KeyDown

Post by dunbarx » Thu Dec 11, 2014 5:03 am

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

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Contact:

Re: KeyUp vs KeyDown

Post by FredBeck » Thu Dec 11, 2014 12:08 pm

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.

ivelink
Posts: 7
Joined: Thu Nov 27, 2014 1:12 pm

Re: KeyUp vs KeyDown

Post by ivelink » Thu Dec 11, 2014 12:43 pm

Thanks.
That makes sense.
I will try. Hopefully will resolve the issue too.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: KeyUp vs KeyDown

Post by jacque » Thu Dec 11, 2014 7:29 pm

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.
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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: KeyUp vs KeyDown

Post by dunbarx » Thu Dec 11, 2014 11:12 pm

What Jacque said. Switch works similarly, with the "case" (if any) that resolves to "true" being accessed directly. In a button somewhere:

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
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

Post Reply