changing inkey to lower case

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
townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

changing inkey to lower case

Post by townsend » Sat Oct 22, 2011 11:07 pm

As the user enters text, I'm using this routine, to change each character to lower case, on the fly.

Code: Select all

on keyup
     put lower(me) into me
     select after text of me
end keyup
It works okay, until the user, uses the arrow keys to move the cursor back for an edit.
That Select command there, is nice, that you can place the cursor before or after or anywhere in the text.
But how to I query the text field to find out what position in the text, the cursor is currently at?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: changing inkey to lower case

Post by bn » Sat Oct 22, 2011 11:42 pm

Hi townsend,

have a look at rawKeyUp, it catches the arrowKey and all the others, and at selectionchanged / selectedChunk. If the insertion point is just the cursor without a selection then word 4 of the selectedChunk is 1 lower than word 2 of the selectedChunk. E.g char 20 to 19 of field 1. KeyUp does not catch the arrowKey. The selectionChanged handler is for handling the shift key and the arrowKey to make an actual selection or the user click-drags to make a selection.

put this into a test field and have a second field for the output:

Code: Select all

on rawKeyUp theKey
   put the selectedchunk into tChunk
   if word 4 of tChunk < word 2 of tChunk then \
      put "the chunk: " & tChunk & cr & "the key:" && theKey & cr & "no of characters in field: " &  length(me) into field 2
   pass rawKeyUp
end rawKeyUp

on selectionChanged
   put the selectedChunk into field 2
end selectionChanged
Kind regards

Bernd

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: changing inkey to lower case

Post by townsend » Wed Oct 26, 2011 8:58 pm

Thanks for the code Bernd-- This is a good example of-- 'the selectedchunk' function.

Code: Select all

on rawKeyUp theKey
     put the selectedchunk into tChunk
     put "keyword 'the selectedchunk' =" &  cr & tChunk & cr & cr into fld "status"
     put "the key code is:" && theKey & cr after fld "status"
     put "# of characters in field: " &  length(me) after fld "status"
     pass rawKeyUp
end rawKeyUp
Now I see how this works.
137 rawkey.jpg
137 rawkey.jpg (30.35 KiB) Viewed 5044 times
Let me ask you this. When I'm using: 'on keyDown' or the 'on rawKeyDown' the inkey value has yet to be displayed. But when I change the value of the passed inkey, it does not change the value of the original key press. The original key press always shows up in the field. Is there any way, not only to capture the inkey, but change it before it appears in the field?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: changing inkey to lower case

Post by mwieder » Wed Oct 26, 2011 9:10 pm

There is, but you have to do it very explicitly yourself. Two things to do: first, set the character into the field, and (very importantly) *don't* pass the message at the end. Something like this:

Code: Select all

on keyUp pKey
     if pKey is space then
          -- for the simple case where it's just after the existing text
          put "*" after me
     end if
     -- don't pass keyUp
     -- pass keyUp
end keyUp

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: changing inkey to lower case

Post by townsend » Thu Oct 27, 2011 6:43 pm

Okay-- thanks.

Here's the final code on converting to lowercase on the fly.

Code: Select all

on keydown inkey
     put the selectedchunk into tChunk
     put word 4 of tchunk into pos
     if chartonum(inkey) >= 65 and chartonum(inkey) <=90 then
          put lower(inkey) after char pos of me
     else
          pass keydown
     end if
end keydown

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: changing inkey to lower case

Post by Mark » Fri Oct 28, 2011 12:51 am

Hi,

I think you should be able to do this directly:

Code: Select all

set the caseSensitive to true
if inkey >= "A" and inkey <="Z" then
  -- etc
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: changing inkey to lower case

Post by townsend » Fri Nov 18, 2011 8:26 pm

There's still one thing I'm having a problem with. I has to do with the instance where a user highlights a couple of letters in the middle of a word, and wants to replace them with another couple of letters.

Code: Select all

if length(the selectedtext of me) >0 then
          replace (the selectedtext of me) with lower(inkey) of me
end if
This code replaces the first letter okay, but because the cursor ends up being positioned at the beginning of the field, the second letter ends up in the wrong place. I know the position, where the cursor should be, but don't know how to position it there.

Is there a way to move a cursor within a field, with code?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: changing inkey to lower case

Post by Mark » Fri Nov 18, 2011 8:33 pm

Hi,

You can set the cursor in a field with

Code: Select all

select after char x of fld y
Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply