Assigning a keyboard shortcut to a button

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

bambster
Posts: 4
Joined: Thu Mar 29, 2018 4:26 pm

Assigning a keyboard shortcut to a button

Post by bambster » Thu Mar 29, 2018 4:43 pm

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

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Assigning a keyboard shortcut to a button

Post by bogs » Thu Mar 29, 2018 6:28 pm

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 9074 times
Image

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Assigning a keyboard shortcut to a button

Post by jmburnod » Thu Mar 29, 2018 6:42 pm

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
https://alternatic.ch

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Assigning a keyboard shortcut to a button

Post by richmond62 » 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
Here's the stack.
(24.08 KiB) Downloaded 227 times

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Assigning a keyboard shortcut to a button

Post by FourthWorld » Thu Mar 29, 2018 7:18 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Assigning a keyboard shortcut to a button

Post by jmburnod » 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
https://alternatic.ch

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Assigning a keyboard shortcut to a button

Post by richmond62 » Thu Mar 29, 2018 7:41 pm

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)
Last edited by richmond62 on Thu Mar 29, 2018 7:59 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Assigning a keyboard shortcut to a button

Post by richmond62 » Thu Mar 29, 2018 7:44 pm

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)

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Assigning a keyboard shortcut to a button

Post by bogs » Thu Mar 29, 2018 8:35 pm

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
Last edited by bogs on Thu Mar 29, 2018 8:42 pm, edited 1 time in total.
Image

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Assigning a keyboard shortcut to a button

Post by richmond62 » Thu Mar 29, 2018 8:42 pm

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.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Assigning a keyboard shortcut to a button

Post by bogs » Thu Mar 29, 2018 8:45 pm

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).
Image

bambster
Posts: 4
Joined: Thu Mar 29, 2018 4:26 pm

Re: Assigning a keyboard shortcut to a button

Post by bambster » Thu Mar 29, 2018 10:20 pm

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!

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Assigning a keyboard shortcut to a button

Post by richmond62 » Fri Mar 30, 2018 8:19 am

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)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9250
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Assigning a keyboard shortcut to a button

Post by richmond62 » Fri Mar 30, 2018 8:20 am

Adding key combinations would be a nice plus though
BP2.png
Button Press 2.zip
Here's the stack.
(23.8 KiB) Downloaded 182 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

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Assigning a keyboard shortcut to a button

Post by AxWald » Fri Mar 30, 2018 1:38 pm

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!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”