Three things wrong with "ask"

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Three things wrong with "ask"

Post by gyroscope » Tue Apr 07, 2009 2:15 pm

Hallo, there's three things wrong with some code I'm using; I'm sure there something obvious here that I've missed perhaps. The code is:

Code: Select all

on mouseDown
if the controlKey is not down then
beep
end if

  if the controlKey is down then
    ask "Change button title" with "Cancel" and "Change"
    set the name of me to it
  else
    exit mouseDown
    end if
end mouseDown
• The first thing is the word "false" appears in the field; always a sign there's something wrong
• The second thing is that if I press cancel, it deletes the name of the button
• The third thing is if the control key is down, it still beeps.

Apart from that, this simple bit of code is alright! :wink:

Any help appreciated, thanks.

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Tue Apr 07, 2009 3:56 pm

If you can live without custom labels on the buttons, then just leave out the "with" part. "With" with ask provides the default response not the buttons. In your case this has been interpreted as a boolean "Change or Cancel" for evaluation - and without any presetting is false - hence false appears as the default response.
If the Cancel button is pressed in an ask dialog, then the result is "Cancel" and the it variable is set to empty - hence the clearing of the label.
Try:

Code: Select all

on mouseDown 
   if the controlKey is not down then 
      beep
   else
      ask "Change button title"
      if the result is not "Cancel" then 
         set the name of me to it
      end if
   end if
end mouseDown
HTH :)

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Tue Apr 07, 2009 4:56 pm

Ah, I understand, SparkOut, thank you for your explanation and revised code. One thing I forgot was that "ask" gives you the Cancel and OK buttons by default.

:)

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

Post by bn » Tue Apr 07, 2009 7:03 pm

Hi gyroscope,

why do you put this into an on mousedown handler? I rarely use this for buttons, the user can change his mind when pushing the button and release it outside the rect of the button, that way the mouseUp does not get triggerd. That is the usual interface convention.

What are you trying to do: what are the names of the buttons supposed to look like?
Wouldnt you rather prefer to set the title of the button, that way you can still refer to the name in a script, it is a lot easier to maintain and debug then to change the name of the button.
If you want to your stack to be cross platform then the mac users rarely use the controlKey, it is the optionskey (alt) instead.
Lastly, you might want to catch the situation when a user clicks ok but has not put any text into the ask box, that returns empty and the name of your buttton will be set to empty.

Code: Select all

on mouseDown
    if (not the controlkey is down) or (not the optionkey is down ) then
        beep
    else
        ask "Change button title"
        if the result is not "Cancel" and it is not "" then
            set the name of me to it
           -- set the title of me to it
        end if
    end if
end mouseDown
If you want to give the user two options plus the option to bail out (with cancel) then you could do this with

Code: Select all

on mouseUp
    if (not the controlkey is down) or (not the optionkey is down ) then
        beep
    else 
        answer "Change button title" with "Cancel" or "red" or "blue"
        if it is not "Cancel" then
            set the title of me to it
        end if
    end if
end mouseUp
If you want to give the user more than two options then how about an option menu: the user understands this and no typos that your script can not control for. You can query this with

Code: Select all

put the label of btn "option menu" into myVar
if you dont want to go for the menupick of the option menu.

Then again you easily might have something special in mind that I dont know. Then forget about this all.

sorry for this lengthy post but I have the feeling it is easier to sort this out early in a project.
regards
Bernd

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Tue Apr 07, 2009 10:45 pm

Hi Bernd
why do you put this into an on mousedown handler?
This is for the Script Collector that you helped so well with, as did SparkOut who supplied the swappable buttons code. So I'd be using mouseUp for normal button use (all to go to a certain card) and then use control (or option now as per your suggestion) with mouseDown to drag/swap them. Then I thought I'd keep it consistent and have mouseDown with the command key for the user to rename the buttons if required.

But I understand and agree with your logic in the majority of cases whereby mouseUp gives the user the chance to change their mind...
Wouldnt you rather prefer to set the title of the button, that way you can still refer to the name in a script, it is a lot easier to maintain and debug then to change the name of the button.
Good thinking there Bernd, I'll do that.
Lastly, you might want to catch the situation when a user clicks ok but has not put any text into the ask box, that returns empty and the name of your buttton will be set to empty.
That's good, I hadn't thought of that situation.

Thank you for your help!

:)

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Wed Apr 08, 2009 2:01 am

For interest, Rev didn't seem to like:

Code: Select all

 if (not the controlkey is down) or (not the optionkey is down ) then  
The control key code wasn't getting through for some reason. I tried quite a few variations on a theme without success, but then sorted it a different way. So for the record, I put:

Code: Select all

on mouseDown 
   if not (the commandkey is down) then
      check2
   else
      grab me ---swapping buttons code
   end if

end mouseDown

on check2
   if not (the optionkey is down) then
      beep ---go to card no. ---
   end if
   
   if the optionKey is down then
      ask "Change button title" 
      if the result is not "Cancel" or it is not "" then 
         set the title of me to it 
      end if 
   end if
end check2
Which seems to do what's required.

Thank you again SparkOut and Bernd.

:)

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

Post by bn » Wed Apr 08, 2009 10:25 am

Hi Gyroscope,
sorry, I goofed the boolean up, (happens to me all the time)
I tested the code and it worked for control and the alt key but also fired when no modifier key was down then it took that for an altkey down.

this works for me and is easier to read and maintain.

Code: Select all

on mouseDown
    if not (the controlkey is down or the optionkey is down) then
        beep
    else
        put keysdown() into temp
        if temp is "" then put "Option- or Alt-Key" into temp
        answer temp
    end if
end mouseDown
on a mac keysdown dont pass a number for the optionkey(unless another not modifier key is also pressed) , according to the documentation it shouldnt pass it for the control key either but it does.

good luck with your project
Bernd

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Wed Apr 08, 2009 5:27 pm

Neat bit of code, Bernd.
good luck with your project
Thank you! I hope to have it ready in a month or two time. Hopefully Forum members will beta-test it for me and give some feedback; then when I'm confident enough with it, I'll "donate" it to the Rev community (well, Forum members at least!)

:)

Post Reply