Block the return key
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Block the return key
Hi all
Is it possible to prevent a user from pressing the return key in a text field? I know you can set the property "tab on return" but I don't want to use this.
Regards
Terry
Is it possible to prevent a user from pressing the return key in a text field? I know you can set the property "tab on return" but I don't want to use this.
Regards
Terry
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Re: Block the return key
Put the following handler in your field script:
Note that the user can still paste multi-line text, so the above isn't full-proof.
HTH,
Jan Schenkel.
Code: Select all
on returnInField
-- do not pass
end returnInField
on enterInField
-- do not pass
end enterInField
HTH,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
Re: Block the return key
Thanks Jan. Didn't realise it was that simple. I'm hoping these users wouldn't be silly enough to paste multiple lines and would accept the consequences of a crash if they did. There is also a "restore default" button that repopulates the field.
I wondered if it was possible to identify the object (in this case a variable) where an error is occurring and then handle it using "on errorDialog"? That should, in theory, make it fairly bulletproof.
Regards
Terry
I wondered if it was possible to identify the object (in this case a variable) where an error is occurring and then handle it using "on errorDialog"? That should, in theory, make it fairly bulletproof.
Regards
Terry
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Re: Block the return key
Well, you can always prevent paste and drag-drop problems by amending the field script as follows:
This should prevent the user from typing in return or enter characters, and strip them away after a paste or drop operation.
HTH,
Jan Schenkel.
Code: Select all
on returnInField
-- do not pass
end returnInField
on enterInField
-- do not pass
end enterInField
on pasteKey
send "StripReturns" to me in 0 milliseconds
pass pasteKey
end pasteKey
on dragDrop
send "StripReturns" to me in 0 milliseconds
pass dragDrop
end dragDrop
on StripReturns
replace return with space in me
end StripReturns
HTH,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
Re: Block the return key
A simpler solution would be to just use the 'textChanged' message to see if a return character has been typed - like this:
Note that the result may be less 'elegant' than the solution proposed by Jan, because once the replace command is executed, you will loose the location of the cursor in the field - ie., the selection will 'jump' to the beginning of the field if a return is typed or pasted. To avoid this, you'd have to add code that saves and restores the selection position after the replace - but it can be done! 
Code: Select all
on textChanged
if return is among the characters of me then replace return with space in me
end textChanged

Re: Block the return key
Thanks to you both. I like the simplicity of the textchanged option which I have used to trigger an answer dialog. Keeping the position of the cursor, etc. will not be an issue as the trapping of any invalid entry sends an instruction to another button to restore the default content of the field.
Regards
Terry
Regards
Terry