detecting capslock key down

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

detecting capslock key down

Post by rodneyt » Sun Jul 11, 2021 1:37 am

Is the only way to detect if the capslockkey is down to monitor its state continuously? e.g. via a function called with send every second or so?

E.g. here and elsewhere in the archives
viewtopic.php?f=9&t=31626

Seems a rather inelegant way of doing things - I hate having to poll state....

There's no key event we can use to monitor capslock state?

~ Rodney

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

Re: detecting capslock key down

Post by dunbarx » Sun Jul 11, 2021 1:58 am

Hi.

Unlike to the other "control"-style keys, option, command, etc. which have messages sent when you press them, the capsLock key does not seem to have its own.

Only a function, the "capsLockKey" tells you what that key is up to, and functions have to be explicitly queried by you under script control.

Craig

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7214
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: detecting capslock key down

Post by jacque » Sun Jul 11, 2021 4:51 pm

I see there's also an eventCapsLockKey function that tells you if caps lock was enabled when an event began, as opposed to the current time. Depending on what you need, that might work. The dictionary isn't very clear about what an event is in this case.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: detecting capslock key down

Post by richmond62 » Sun Jul 11, 2021 5:56 pm

Code: Select all

on keyUp KUP
   if the capsLockKey is down then 
      put "DOWN" into fld "ff"
   else
      put "UP" into fld "ff"
      end if
end keyUp
-
SShot 2021-07-11 at 19.53.37.png
SShot 2021-07-11 at 19.53.37.png (10.28 KiB) Viewed 4668 times
-
SShot 2021-07-11 at 20.03.35.png
-
The Dictionary is your friend.
Attachments
CapsLocker.livecode.zip
Here's the stack.
(895 Bytes) Downloaded 111 times

stam
Posts: 2633
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: detecting capslock key down

Post by stam » Sun Jul 11, 2021 7:01 pm

Looks like the options are to either check the state of capslockKey with other events like keyUp, rawKeyDown, etc, or poll for it at regular intervals.
Whats' the context if you don't mind me asking?

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

Re: detecting capslock key down

Post by richmond62 » Sun Jul 11, 2021 7:57 pm

I wonder if you can do this sort of thing:

pseudoCode

Code: Select all

send "keyUp" to key "y"
?

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: detecting capslock key down

Post by rodneyt » Sun Jul 11, 2021 11:13 pm

Thanks for the replies, interesting about eventCapsLockKey Jacque, I hadn't run across that one.

I have a small productivity app I am building and I am trying to support context switching with minimal keyboard use. Caplock is a very large, single, lit, key and in my observation not much used by people - so it's ideal for frequent use. Easier to reach than function keys (which are often tiny or need modifiers to access these days).

But it doesn't generate events, it can only be polled via the supported function calls (unless user types a key with capslock down, but I am interested in operation of the capslock key itself).

I am going to add a feature request for capslock key event.

~ Rodney

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

Re: detecting capslock key down

Post by richmond62 » Mon Jul 12, 2021 6:49 am

You can poll the state of the capsLock key right now . . .
-
Screen Shot 2021-07-12 at 8.45.47 AM.png
Have a button that contains this sort of code:

Code: Select all

on mouseUp
   if the capsLockKey is down then
      put "DOWN" into fld "ff"
   else
      put "UP" into fld "ff"
   end if
end mouseUp
and then, in your function / mainscript this:

Code: Select all

send "mouseUp" to btn "checkCapsL"
obviously you are unlikely to dump the result into a field as my example does . . .
Attachments
capsLox.livecode.zip
Here's the stack.
(1000 Bytes) Downloaded 127 times

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: detecting capslock key down

Post by rodneyt » Mon Jul 12, 2021 7:13 am

richmond62 wrote:
Mon Jul 12, 2021 6:49 am
You can poll the state of the capsLock key right now . . .
...
Yes, but as I noted above, it's an inelegant approach....

But it seems, the only way to do it at the moment.

~ Rodney

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: detecting capslock key down

Post by rodneyt » Mon Jul 12, 2021 7:14 am

For those who are curious, you can detect state of control keys including capslock from applescript

Code: Select all

use AppleScript version "2.4" -- The ability to use ASObjC in running scripts was introduced in Mac OS 10.10.
use framework "Foundation"
use framework "AppKit" -- NSEvent is an AppKit class.
use scripting additions -- In case needed.

on modifierKeysPressed()
	set modifierKeysDOWN to {capslock_down:false, command_down:false, option_down:false, control_down:false, shift_down:false}
	
	set currentModifiers to current application's class "NSEvent"'s modifierFlags()
	tell modifierKeysDOWN
		-- 65536 capslock key
		set its capslock_down to (currentModifiers div 65536 mod 2 is 1)
		-- 524288: NSAlternateKeyMask constant in Mac OS 10.10 & 10.11/NSEventModifierFlagOption thereafter.
		set its option_down to (currentModifiers div 524288 mod 2 is 1)
		-- 1048576: NSCommandKeyMask/NSEventModifierFlagCommand.
		set its command_down to (currentModifiers div 1048576 mod 2 is 1)
		-- 131072: NSShiftKeyMask/NSEventModifierFlagShift.
		set its shift_down to (currentModifiers div 131072 mod 2 is 1)
		-- 262144: NSControlKeyMask/NSEventModifierKeyControl.
		set its control_down to (currentModifiers div 262144 mod 2 is 1)
	end tell
	
	return modifierKeysDOWN
end modifierKeysPressed

modifierKeysPressed()


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

Re: detecting capslock key down

Post by richmond62 » Mon Jul 12, 2021 7:28 am

an inelegant approach....
Well, inelegant is as inelegant does, a matter of subjective aesthetics, something when it comes to programming
I don't bother with; I just attempt to get a job done.

But, then, I have never really bothered about cars: a Lada will get you to Moscow just as will a Merc.
-
car.jpg
you can detect state of control keys including capslock from applescript
I would not doubt that for a moment, but as soon as you leave "planet Apple" that will do
you not a whit of good at all.
Last edited by richmond62 on Mon Jul 12, 2021 5:43 pm, edited 1 time in total.

stam
Posts: 2633
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: detecting capslock key down

Post by stam » Mon Jul 12, 2021 8:27 am

rodneyt wrote:
Mon Jul 12, 2021 7:14 am
For those who are curious, you can detect state of control keys including capslock from applescript
...
Interesting, but how is this better than using shiftLockKey to detect the state of capslock?

------
EDIT: the above is a typo/autocorrect: it should read: how is this better than using capsLockKey() to detect the state of capslock?
Last edited by stam on Mon Jul 12, 2021 2:05 pm, edited 2 times in total.

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

Re: detecting capslock key down

Post by richmond62 » Mon Jul 12, 2021 10:30 am

using shiftLockKey to detect the state of capslock?
?

Surely you would use shiftLockKey on something like a BBC micro that actually has a shift lock key?

ALSO: one should not confuse a shiftLock key for a capsLocks key.
-
shiftLock.jpg
-
ALSO shiftLock does NOT occur in the LiveCode dictionary.

stam
Posts: 2633
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: detecting capslock key down

Post by stam » Mon Jul 12, 2021 2:02 pm

richmond62 wrote:
Mon Jul 12, 2021 10:30 am
ALSO: one should not confuse a shiftLock key for a capsLocks key.
Well done Richmond... you spotted my typo! Congrats ;)
Good to know you're paying attention...

I of course meant capsLockKey() to detect the state of the capsLock.

Given that we're talking about the capslockkey and capslock and more capslock, it should't really be a surprise this was an error.
Well done on finding yet another picture to post though...

To re-iterate, in case there is any misunderstanding (and lest i give Richmond yet another excuse to post pictures):
I did not mean to type shiftLock! Sorry!

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

Re: detecting capslock key down

Post by SparkOut » Mon Jul 12, 2021 2:16 pm

But shiftLock happens.
:cry:
I miss Hermann

Post Reply

Return to “Talking LiveCode”