What is being left in a field after hitting return

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

What is being left in a field after hitting return

Post by quailcreek » Mon Mar 01, 2010 3:19 am

Hello All,
I’m running into something odd and I hope someone else has encountered this so you can tell me what I’m doing wrong. Hopefully before I go completely nuts.

First here’s what I’m doing:
From a dropdown menu, built with the menu builder with the standard switch structure, I’m executing a script that runs a VB script. The VB script requires a numeric entry to run correctly. I’m gathering this value from a sub-stack window that is opened as modal from the menu script. On the sub-stack window I have a field and 2 buttons. The field is where the numeric entry comes from. What I want is for the user to either press the OK button or the return key to confirm the numeric entry and execute the script. Here’s the odd part. If the field is empty and I hit the return key everything is fine and the beep is heard. However, if I hit the return key again the script executes with an error in the VB script. It seems to be leaving something in the field. But, if I hit the backspace key before hitting the return key the second time I again get the beep. What is being left in the field when I press the return key?

Tom

This is the OK button script.
global gRowCount – value used in the VB script
ON mouseUp
IF field "Number of rows" is empty THEN
put empty into field "Number of rows"
put empty into gRowCount
beep
ELSE
put field "Number of rows" into gRowCount
put empty into field "Number of rows"
close this window
END IF
END mouseUp

This is the card script. The only card in the sub-stack
global gRowCount
ON preOpenCard
put empty into gRowCount
put empty into field "Number of Rows"
focus field "Number of rows"
pass preOpenCard
END preOpenCard

This is the field script
global gRowCount
ON keyDown tKey
delete the selectedtext
IF tKey is an integer THEN pass keydown – only allows numbers to be entered
beep
END keyDown

ON enterInField
returnInField
pass enterInField
END enterInField

ON returnInField
send mouseUp to button "OK"
pass returnInField
END returnInField
Tom
MacBook Pro OS Mojave 10.14

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: What is being left in a field after hitting return

Post by Regulae » Mon Mar 01, 2010 4:26 am

When you press the return key in the field, your script sends a “mouseUp” to the “OK” button, whose script clears the field- so you would expect the field to be empty after this. In fact, a return character is passed to the field, because of the instruction order:

Code: Select all

ON returnInField
   send mouseUp to button "OK"
   pass returnInField
END returnInField
... which has the button “OK”s script execute, clearing the field, but after that the return character is passed to the field, and thus the field is no longer empty, and is also non-numeric. Removing the “pass” will prevent this, thus:

Code: Select all

ON returnInField
   send mouseUp to button "OK"
END returnInField
Regards,

Michael

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: What is being left in a field after hitting return

Post by quailcreek » Mon Mar 01, 2010 5:03 am

Thank you, Michael. I thougth I was going crazy.
Tom
MacBook Pro OS Mojave 10.14

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: What is being left in a field after hitting return

Post by quailcreek » Mon Mar 01, 2010 5:16 am

So Michael let me ask you this. What is a good rule of thumb for when to add passXxxx to a script?
Tom
MacBook Pro OS Mojave 10.14

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: What is being left in a field after hitting return

Post by Regulae » Mon Mar 01, 2010 7:02 am

Interesting question- it seems to depend so much on the specifics of the situation. I always have in mind that, unlike when the user presses the mouse, a key press causes a default action- a character is typed. As we normally want this to happen, we normally pass it. But it is clear from your script that that’s exactly what you were controlling- filtering for numbers only and having “return” redefined to OK the entry. To be honest, I only saw what was going on by setting up an example using your scripts- my field “Number of rows” was multi-line, so I saw the cursor jump to the next line, which is what I thought must be happening. Evidently the very last thing that happens with a keyDown/returnInField is the actual typing of a character. What’s interesting is the “send” command. I remember from the Rev dictionary:
“If you don't specify a time, the message is sent immediately, and any handler it triggers is completed before the rest of the current handler is executed.”
... when you put all this together it becomes clear why the returnInField should not be passed in this case. It is easy for me to sound wise after the event- the problem would have been very hard to spot (you can’t see a “return”) and if your field “Number of rows” showed only a single line, you’d be lucky to notice where the blinking insertion point had gone. All the ingredients for puzzlement/frustration. This is the sort of problem scenario I’ll file away in the back of my mind as one of the benefits of being involved on the Rev forum.

P.S. I was working on a laptop without an "enter" key. Having plugged in an external keyboard, you will also want to remove "pass enterInField".

Post Reply