Page 1 of 3

detecting capslock key down

Posted: Sun Jul 11, 2021 1:37 am
by rodneyt
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

Re: detecting capslock key down

Posted: Sun Jul 11, 2021 1:58 am
by dunbarx
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

Re: detecting capslock key down

Posted: Sun Jul 11, 2021 4:51 pm
by jacque
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.

Re: detecting capslock key down

Posted: Sun Jul 11, 2021 5:56 pm
by richmond62

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 7000 times
-
SShot 2021-07-11 at 20.03.35.png
-
The Dictionary is your friend.

Re: detecting capslock key down

Posted: Sun Jul 11, 2021 7:01 pm
by stam
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?

Re: detecting capslock key down

Posted: Sun Jul 11, 2021 7:57 pm
by richmond62
I wonder if you can do this sort of thing:

pseudoCode

Code: Select all

send "keyUp" to key "y"
?

Re: detecting capslock key down

Posted: Sun Jul 11, 2021 11:13 pm
by rodneyt
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

Re: detecting capslock key down

Posted: Mon Jul 12, 2021 6:49 am
by richmond62
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 . . .

Re: detecting capslock key down

Posted: Mon Jul 12, 2021 7:13 am
by rodneyt
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

Re: detecting capslock key down

Posted: Mon Jul 12, 2021 7:14 am
by rodneyt
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()


Re: detecting capslock key down

Posted: Mon Jul 12, 2021 7:28 am
by richmond62
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.

Re: detecting capslock key down

Posted: Mon Jul 12, 2021 8:27 am
by stam
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?

Re: detecting capslock key down

Posted: Mon Jul 12, 2021 10:30 am
by richmond62
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.

Re: detecting capslock key down

Posted: Mon Jul 12, 2021 2:02 pm
by stam
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!

Re: detecting capslock key down

Posted: Mon Jul 12, 2021 2:16 pm
by SparkOut
But shiftLock happens.
:cry:
I miss Hermann