Page 1 of 5

Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 4:43 pm
by bambster
Hey!

I'm a total beginner - not experienced at all in coding but I've been fiddeling around with Live Code for quite some time now and got my first few applications up and running.

One thing that I wasn't quite able to figure out is the following:

I have a couple of buttons that I'd like to be activated (on mousedown functions inside...) via the press of a keyboard key, or combination of keys (e.g. Strg + Alt + e) in addition to the 'on mouse down'-action. I am using an external USB input device fires those keyboard 'macros' on button press, but I don't know how to tell my Live Code buttons how to detect inputs other than 'mouse down'.

I stumbled upon the 'keyDown' function but can't seem to make it work the same way as the 'on mouse down' function.

Any help or nudge in the right direction is highly appreciated

Thanks in advance!
Bambster

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 6:28 pm
by bogs
You didn't mention which version of Lc your working in, but regardless what you are looking for can be handled directly in the properties window, by setting the 'accelKey' property.
Selection_003.png
Selection_003.png (21.61 KiB) Viewed 9360 times

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 6:42 pm
by jmburnod
Hi,
I think you have to use keysdown function
The keysDown function returns a list of keycodes of pressed keys, separated by commas if more than one key is being pressed.

Something like that:

Code: Select all

on mousedown 
   if the optionkey is down and the keysdown = 101,0 then
      answer  "optionkey is down and you press e" -- e = 101
   else
      "nothing happened"
   end if
end mousedown

You may get the keycode of a touch with this on card script

Code: Select all

on rawkeydown pKey
   put pKey
end rawkeydown 
Best regards
Jean-Marc

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 6:51 pm
by richmond62
Here's a way to do this:
BPress.png
The button "Button F1" contains this script:

Code: Select all

on mouseUp
   put "You just clicked on button Button F1."
end mouseUp
The card contains this script:

Code: Select all

on rawKeyUp RUP
   if RUP = 65470 then
      send "mouseUp" to btn "Button F1"
   else
      pass rawKeyUp
      end if
end rawKeyUp
Button Press.livecode.zip
Here's the stack.
(24.08 KiB) Downloaded 233 times

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 7:18 pm
by FourthWorld
If you want to provide visual feedback for the action the keyboard shortcut triggers, you can use "click at the loc of button..." instead of "send...", so the button will momentarily highlight like it would if clicked.

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 7:23 pm
by jmburnod
Hi Bogs,
I have forgotten this way but I can't find accelKey in my object inspector (LC indy 8.1.6, OS X)
Best
Jean-Marc

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 7:41 pm
by richmond62
shortCuts.png
Here's the 7.1.4 Props palette on the left, and the 8.1.8 Props palette on the right.

First off: the possibility of assigning keystrokes seems to have either been "deprecated"
(why do I dislike that word so much?) or, at least, made inaccessible.

Secondly: what "ancient" flavour of LiveCode was your screen snap from,
as in 7.1.4 the thing is called a "shortcut" and not an "accelKey"? :wink:

I just performed some archaeology and found that the Props palette for RunRev 4.0 uses "shortcut".

Oddly enough so does RunRev 2.2.1 (Linux).

Maybe that's a Windows 'thing' 8)

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 7:44 pm
by richmond62
Aha: one can set an accelKey via code in LiveCode 8.1.8.

I expect in LiveCode's efforts to make LiveCode 8.1.8 easier for new learners they hid that feature away. 8)

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 8:35 pm
by bogs
jmburnod wrote:
Thu Mar 29, 2018 7:23 pm
Hi Bogs,
I have forgotten this way but I can't find accelKey in my object inspector (LC indy 8.1.6, OS X)
Best
Jean-Marc
richmond62 wrote:
Thu Mar 29, 2018 7:41 pm
First off: the possibility of assigning keystrokes seems to have either been "deprecated"
(why do I dislike that word so much?) or, at least, made inaccessible.
Yah, it has been called different things over the releases, figures that something ridiculously easy and useful would be ripped out for some obscure reason. Progress! :roll:
Secondly: what "ancient" flavour of LiveCode was your screen snap from,
Well, when I go poking around for something and I want it fast my default is 6.5.2. Funny you should mention 2.x, I was diddling around with that beaut a lot yesterday, you want to talk about features I find refreshing to have, that one has it in spades. If only I could get it to load faster, it would probably replace 6.5.2 and MC as my default goto button :twisted:
Maybe that's a Windows 'thing' 8)
BITE YOUR TONGUE! Why, even mentioning such is almost BLASPHEMOUS in my case :P

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 8:42 pm
by richmond62
that one has it in spades
Among many other ways I have made myself popular with LiveCode and the "choir" is by repeatedly
suggesting a bit of a slowdown (sometimes chasing hell-for-leather after new features means old ones that need a bit of polishing get disregarded) and even the odd backward step.

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 8:45 pm
by bogs
You and I are apparently in the same choir. It is what probably keeps making it less desirable to dl the newest versions at all (for me, mind you I am a distinct minority I think).

Re: Assigning a keyboard shortcut to a button

Posted: Thu Mar 29, 2018 10:20 pm
by bambster
richmond62 wrote:
Thu Mar 29, 2018 6:51 pm
Here's a way to do this:

BPress.png

The button "Button F1" contains this script:

Code: Select all

on mouseUp
   put "You just clicked on button Button F1."
end mouseUp
The card contains this script:

Code: Select all

on rawKeyUp RUP
   if RUP = 65470 then
      send "mouseUp" to btn "Button F1"
   else
      pass rawKeyUp
      end if
end rawKeyUp
Button Press.livecode.zip
Okay, wow. This is basically all I was trying to do. Thank you so much!

Adding key combinations would be a nice plus though, since this would help prevent accidental key presses, but for now this really is all I needed.

Also big thanks to all the others contributors!

Re: Assigning a keyboard shortcut to a button

Posted: Fri Mar 30, 2018 8:19 am
by richmond62
You and I are apparently in the same choir
Much as I have tried, all my life, I have never managed to sing in tune. 8)

Re: Assigning a keyboard shortcut to a button

Posted: Fri Mar 30, 2018 8:20 am
by richmond62
Adding key combinations would be a nice plus though
BP2.png
Button Press 2.zip
Here's the stack.
(23.8 KiB) Downloaded 192 times
In the cardScript:

Code: Select all

on rawKeyUp RUP
   if the shiftKey is down then
   if RUP = 65470 then
      send "mouseUp" to btn "Button Shift + F1"
   else
      pass rawKeyUp
   end if
else
   if RUP = 65470 then
      send "mouseUp" to btn "Button F1"
   else
      pass rawKeyUp
   end if
   end if
end rawKeyUp

Re: Assigning a keyboard shortcut to a button

Posted: Fri Mar 30, 2018 1:38 pm
by AxWald
Hi,
richmond62 wrote:
Thu Mar 29, 2018 7:41 pm
[...] the thing is called a "shortcut" and not an "accelKey"? [...]
This depends of the setting in the preferences:
"Property labels are:"

"Description of option" => "Shortcut"
"Name of LC property" => "accelKey"

And yes, they omitted it completely in the 8 versions prop palette. Who needs keyboard shortcuts in "apps", anyways?

Have fun!