Using BackSpaceKey and selectedChunk to Analyze a Word

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: Klaus, FourthWorld, heatherlaine, kevinmiller

Post Reply
Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm
Location: Heidelberg, Germany

Using BackSpaceKey and selectedChunk to Analyze a Word

Post by Gage » Thu Mar 19, 2015 7:27 am

Hello!

I am trying to track a user's Backspace key presses in order to remove a link from a previously linked word in a field.

I know of the selectedChunk function, but I'm not really sure how I can use it in this capacity.

Here is some pseudo-script of what I want to accomplish, which would live in the field's script:


on BackSpaceKey -- When the Backspace key is pressed
put ... into tWordBeforeCursorPosition -- I don't know how I would get the word adjacent to the cursor position into a variable... selectedChunk, maybe?

if the linkText of tWordBeforeCursorPosition is not empty then -- Check if the word that will be altered by the Backspace action has its linkText set
set the linkText of tWordBeforeCursorPosition of field "MyField" to empty -- It does, so remove the link
exit BackSpaceKey -- exit handler without passing the message, so the user must press Backspace twice to remove the link AND the characters of the word

else
pass BackSpaceKey -- It does not, so Backspace as usual
end if
end BackSpaceKey


Hopefully my script example makes it very clear what I'm trying to do. Let me know if I need to further clarify anything!

Thanks!
Phil E.

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Using BackSpaceKey and selectedChunk to Analyze a Word

Post by magice » Thu Mar 19, 2015 1:50 pm

This is kind of an ugly way to do it, but it works provided the cursor is exactly to the right of the word you want deleted (no space between them). I'm sure someone can come up with a better way. Maybe a regex solution?

Code: Select all

put word 2 of the selectedChunk into x
   put x-1 into x
   repeat until character x of fld "field" is " " or x is 0
      delete character x of fld "field"
      subtract 1 from x
   end repeat
EDIT: OK i just reread the op and I guess I completely misread it the first time. Ignore me, or modify the above to put character x before a variable.

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

Re: Using BackSpaceKey and selectedChunk to Analyze a Word

Post by dunbarx » Thu Mar 19, 2015 3:52 pm

But do you want to delete text as you go? Or delete the linked text when you "enter" it?

I think what you really want is something lile this. Make a field with several words in it. Set the textStyle of an interior word to "link". Put this in its script:

Code: Select all

on arrowKey tkey
   if tKey = "left" then
      if the textStyle of the selectedChunk = "link" then put "link" else put "plain" --or set the textStyle of that chunk to whatever
   end if
   pass arrowkey
end arrowKey
Place the cursor at the end of the text and press the arrow key. Watch the message box. Does this help?

Craig Newman

asayd
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 95
Joined: Mon Apr 10, 2006 7:02 pm
Location: Provo, Utah, USA
Contact:

Re: Using BackSpaceKey and selectedChunk to Analyze a Word

Post by asayd » Thu Mar 19, 2015 3:59 pm

Phil,

I'd take a little bit different approach. (Watch for text wrap.)

Code: Select all

on rawKeyDown pKeyCode
  if pKeyCode = 65288 then # code for the backspace key
    put word 2 of the selectedChunk into tCharPos
    if the textStyle["link"] of char tCharPos -1 of fld "MyField" is true then # or maybe 'the effective textStyle["link"]'
      set the textStyle["link"] word (the number of words in char 1 to tCharPos of fld "MyField") of fld "MyField" to false
    end if
  end if
  pass rawKeyDown
end rawKeyDown
This is not tested, but this seems like it should work.

Cheers,

Devin

EDIT: I added the end rawKeyDown that I'd forgotten in the initial post.
Last edited by asayd on Thu Mar 19, 2015 8:47 pm, edited 1 time in total.
Devin Asay
Learn to code with LiveCode University
https://livecode.com/store/education/

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm
Location: Heidelberg, Germany

Re: Using BackSpaceKey and selectedChunk to Analyze a Word

Post by Gage » Thu Mar 19, 2015 6:12 pm

Good morning all! (My time, anyway)

Magice, I appreciate your response - that actually looks like a good way to at least gather the text I am trying to select into a variable, which is one of my first steps.

Ideally, I would be able to do a really fast check to see if there is a link associated with the character before the cursor position to reduce processing time. If yes, then I'd use script like yours to gather up the whole word and just remove its link on the first Backspace press. Then, when the link has been removed, the backspace would work as normal.

Craig, I want backspace to delete characters as normal, EXCEPT when the character that would be deleted is part of a linked chunk of text. In that case, it would remove the link from that chunk on the first backspace press, but no characters. Thereafter, it would return to the normal behavior.

Sci-Fi example: If you were to imagine a chunk of text as a spaceship, and the Backspace key as a laser blast, I want a link on a chunk of text to act as a shield. If the laser targets any part of the spaceship, it would have to take down the shield first to be able to delete that spaceship component.

I did just test your script, Craig, and yes! that works... almost. I am noticing that if "mytextstring" is linked, the Message Box does not switch from "plain" to "link" until after I press left arrow with the cursor position between "n" and "g", so it is not recognizing the "g" as being linked for some reason. I also tested the left limit of the string, and I notice that the space char preceding the word DOES yield a "link" response in the MB. It's as if the linkText that is supposed to be mapped to char 5-17 (pulling numbers out of thin air, here) is ACTUALLY mapped to char 4-16. Same number of chars, just starting and ending one char early.

Now... Devin! You win! That works perfectly. I added in an "exit rawKeyDown" line after removing the link, so that only one thing happens or the other (EITHER remove link OR remove the char). I also tested a few approaches to be able to give you feedback, as well:

--
if the textStyle["link"] of char tCharPos -1 of fld "MyField" is true then -- This, your original code, works great!
if the textStyle of char tCharPos -1 of fld "MyField" is "link" then -- My idea also works great!
--
if the effective textStyle["link"] of char tCharPos -1 of fld "MyField" is true then -- This swiftly and efficiently crashes LiveCode 7.0.2 (rc 2)... guess it depends on your desired outcome to decide whether this one works as great -.-
--

So, between all of your responses, I've learned some great approaches and have a working script!

Thank you kindly!

Best,
Phil E.

asayd
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 95
Joined: Mon Apr 10, 2006 7:02 pm
Location: Provo, Utah, USA
Contact:

Re: Using BackSpaceKey and selectedChunk to Analyze a Word

Post by asayd » Thu Mar 19, 2015 8:51 pm

if the effective textStyle["link"] of char tCharPos -1 of fld "MyField" is true then -- This swiftly and efficiently crashes LiveCode 7.0.2 (rc 2)... guess it depends on your desired outcome to decide whether this one works as great -.-
Boom! Crashes in 7.0.3 STABLE as well. That's a bug that ought to be reported. If the syntax is wrong it might fail, but it should never crash.

I'll report it if you don't have the time.

Devin
Devin Asay
Learn to code with LiveCode University
https://livecode.com/store/education/

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm
Location: Heidelberg, Germany

Re: Using BackSpaceKey and selectedChunk to Analyze a Word

Post by Gage » Thu Mar 19, 2015 9:33 pm

Devin,

I was thinking it might be... thanks for checking that!

I don't mind making the bug report now.

Thanks again for helping me out :D

Phil E.

Gage
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 224
Joined: Tue Mar 26, 2013 8:59 pm
Location: Heidelberg, Germany

Re: Using BackSpaceKey and selectedChunk to Analyze a Word

Post by Gage » Thu Mar 19, 2015 10:09 pm

Devin and anyone interested,

Bug has been reported. http://quality.runrev.com/show_bug.cgi?id=15030

Cheers,
Phil E.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”