Limiting allowed key stroked in a text entry field

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Limiting allowed key stroked in a text entry field

Post by drhoward » Tue May 12, 2015 8:37 am

I have a text entry field in which I use the handler rawKeyDown to limit the acceptable key strokes to numeric, backspace, delete, right arrow and left arrow. In this handler, I also restrict the entry to the range 0 to 99 (2 digits maximum). It works fine except for the case where there are 2 digits that have been entered either manually or by the program, and I use the mouse to highlight either one or both bytes in the field. The numeric keys then don't function because I already have 2 digits entered and I don't know how to recognize that one or both digits are highlighted, and the "pass rawKeyDown" command shouldn't be skipped. The following is the code that I'm using. Does anyone have any ideas? Thanks!

on rawKeyDown theKeyNumber -- use this routine to restrict the keys that are permitted
put Field VolumeField into sTextBuf
put false into sOKflag
switch (theKeyNumber)
case 48 -- key 0
if length(sTextBuf) = 0 then
put true into sOKflag
break
end if
case 49 -- key 1
case 50 -- key 2
case 51 -- key 3
case 52 -- key 4
case 53 -- key 5
case 54 -- key 6
case 55 -- key 7
case 56 -- key 8
case 57 -- key 9
if length(sTextBuf) < 2 then
put true into sOKflag
end if
break
case 65361 -- key left arrow
case 65363 -- key right arrow
case 65288 -- key backspace
case 65535 -- key delete
put true into sOKflag
break
default
end switch
if sOKflag = true then
pass rawKeyDown
end if
end rawKeyDown

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limiting allowed key stroked in a text entry field

Post by dunbarx » Tue May 12, 2015 2:28 pm

Hi.

Try inserting this line into your handler. I show two lines of yours, with the line to insert:

Code: Select all

   put false into sOKflag
   put  numToChar(theKeyNumber) into the selectedChunk
   switch (theKeyNumber)
Now this needs a bit of work. You only want it to be considered if there is a valid selection in the field, no? There may be other small usage issues to cleanly add this feature to your handler, for example, this gadget does not like a two-digit argument. Test and ye shall find.

Craig Newman

drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: Limiting allowed key stroked in a text entry field

Post by drhoward » Tue May 12, 2015 3:37 pm

I tried what you suggested. Unfortunately, this doesn't appear to address the root of the problem, which is that I still have no way of detecting whether one or both digits in the Field have been highlighted or not. I have to perform different functions if there has been a highlight and if there hasn't. The highlight is a mouse function that is being handled outside of my program. It is not a key stroke function.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limiting allowed key stroked in a text entry field

Post by dunbarx » Tue May 12, 2015 4:50 pm

OK.

Here is another clue:

Code: Select all

  if the length of the selection = 1 then
      put  numToChar(theKeyNumber) into the selectedChunk
   end if
So this takes care of a single char selection. Any ideas on the other case?

Craig

drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: Limiting allowed key stroked in a text entry field

Post by drhoward » Tue May 12, 2015 7:39 pm

I'm sorry! I wasn't clear. This doesn't take care of either single character or double character cases! The only characters allowed are numeric. This allows any character to be inserted.

Is there any way that we can write a command that says something like: if Field VolumeField is highlighted then ..., or if char 1 of Field VolumeField is highlighted then ...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limiting allowed key stroked in a text entry field

Post by dunbarx » Tue May 12, 2015 11:21 pm

OK.

We build on what we know, and what we play with. Like a puzzle, we learn to add parts to make a whole. Try this:

Code: Select all

 if the length of the selection = 1 and numToChar(theKeyNumber) is an integer then
      put  numToChar(theKeyNumber) into the selection
   end if
Now this still begs the issue of the two character case. Can you write back with a strategy or two? We will then work with that.

Craig

drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: Limiting allowed key stroked in a text entry field

Post by drhoward » Wed May 13, 2015 9:02 am

Thank you! Your suggestion worked when there was only 1 digit in the field and a modification of it worked for two digit in the field.

Rather than have 2 compound if statements in a row, I pulled out the common part and made it an initial if statement. The key to making this work was your cluing me in to using the phrase "the length of the selection, which answered the question that I was asking.

The full handler code is: (Hopefully this will be useful for someone else.)

on rawKeyDown theKeyNumber -- use this routine to restrict the keys that are permitted
put false into sOKflag
if numToChar(theKeyNumber) is an integer then
if the length of the selection = 1 then
put true into sOKflag
else
if the length of the selection = 2 then
put true into sOKflag
end if
end if
end if
put Field VolumeField into sTextBuf
switch (theKeyNumber)
case 48 -- key 0
if length(sTextBuf) = 0 then
put true into sOKflag
break
end if
case 49 -- key 1
case 50 -- key 2
case 51 -- key 3
case 52 -- key 4
case 53 -- key 5
case 54 -- key 6
case 55 -- key 7
case 56 -- key 8
case 57 -- key 9
if length(sTextBuf) < 2 then
put true into sOKflag
end if
break
case 65361 -- key left arrow
case 65363 -- key right arrow
case 65288 -- key backspace
case 65535 -- key delete
put true into sOKflag
break
default
end switch
if sOKflag = true then
pass rawKeyDown
end if
end rawKeyDown

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Limiting allowed key stroked in a text entry field

Post by dunbarx » Wed May 13, 2015 4:39 pm

Hi.

The thing to take home from this is that LC has many properties, and these can be exploited to support your programming. You need to know these properties exist, of course.

I am not being flip when I say that if you wish you had a certain property that would make your life easy, it likely exists already. In the issue that stumped you, you might have said to yourself "...how do I know whether one or two chars is hilited?" Well, put this into a button script, hilite some text and click the button:

Code: Select all

on mouseUp
   answer the hilitedText & return & the selectedText  & return &  the selection  & return &  the selectedChunk  & return &  the selectedLoc && the length of the selection
end mouseUp
How would you know to look for these? Practice. How do you use these to advantage, for example, using the "length" function with the "selection" function? Practice.

Craig

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”