Detecting CapsLock State
Moderators: JRL, Dorian (MJT support)
Detecting CapsLock State
Back in March of 2004, tmallardi asked: "Can someone advise me on how I might be able to determine the state of the CapsLock key while running a Macro Scheduler script?"
http://www.mjtnet.com/usergroup/viewtop ... ht=capsoff
I did not see that his question was answered at that time and am wondering if anyone has come up with a solution.
The only way I have come up with is to open notepad, Send Character upper case "A" save the file, read line 1 of the file and then compare the result to an upper case "A" within the script. If they match, capslock is off.
(If capslock is on, the "A" sent to notepad will be lower case and will be found unequal to the upper case "A" specified in the script.)
This works but is not very eloquent since the notepad screen flashes on and off.
Anybody have a better idea?
Thank you,
Dick
http://www.mjtnet.com/usergroup/viewtop ... ht=capsoff
I did not see that his question was answered at that time and am wondering if anyone has come up with a solution.
The only way I have come up with is to open notepad, Send Character upper case "A" save the file, read line 1 of the file and then compare the result to an upper case "A" within the script. If they match, capslock is off.
(If capslock is on, the "A" sent to notepad will be lower case and will be found unequal to the upper case "A" specified in the script.)
This works but is not very eloquent since the notepad screen flashes on and off.
Anybody have a better idea?
Thank you,
Dick
With the commands CapsOn and CapsOff you can ensure caps lock is either on or off, so detecting it's state should not be needed. Unless .. do you have some other reason to need to detect caps lock state? Is it not sufficient to just ensure it is whatever state you want it?
MJT Net Support
[email protected]
[email protected]
I ran into this same problem years ago and worked around it
in the same way, outputting a char then reading it back in through
the clipboard to see if an "a" or an "A" was produced. All that just
to detect the state of the Caps Lock key.
Support, if you're wondering why we'd want to detect the state,
for me it was... I wanted to offer a set of macros triggered by
hot keys that would accomplish certain tasks for a user on their PC
but when the macro they invoked was done... it would leave Caps Lock
in the state it was before they hit that hot key.
Some users always had their Caps Lock on, some always had it off.
I had to detect what it was when the macro started, in order to restore
that state after if necessary.
My vote would be to add System Variables we could read at any time
to detect the current state of Caps Lock, Scroll Lock and Num Lock.
What do you think?
in the same way, outputting a char then reading it back in through
the clipboard to see if an "a" or an "A" was produced. All that just
to detect the state of the Caps Lock key.
Support, if you're wondering why we'd want to detect the state,
for me it was... I wanted to offer a set of macros triggered by
hot keys that would accomplish certain tasks for a user on their PC
but when the macro they invoked was done... it would leave Caps Lock
in the state it was before they hit that hot key.
Some users always had their Caps Lock on, some always had it off.
I had to detect what it was when the macro started, in order to restore
that state after if necessary.
My vote would be to add System Variables we could read at any time
to detect the current state of Caps Lock, Scroll Lock and Num Lock.
What do you think?
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

I have an ERP application written in COBOL that operates in a "runtime" under Windows OSs. It does not accept copy or paste of text. I use MS to "paste" text into fields within that application. The ERP app requires that you enter the data in upper case. Most users are in the software all day and have their caps lock on. A few, like me, usually have their caps lock off. In the scripts, I use "capson" and exit the scripts that way because it is the more common state. Invariably, I'll run one of these scripts then go to email and start typing with caps on. Not a big deal but irritating. Also difficult to accept that I don't seem to be trainable.
Also, I would think that anyone writing programs that are used outside their sphere of influence would be required to set key lock states back to their original condition on exit of their programs.
Thanks for listening,
Dick
Also, I would think that anyone writing programs that are used outside their sphere of influence would be required to set key lock states back to their original condition on exit of their programs.
Thanks for listening,
Dick
The next release, due Monday, lets you run functions in DLLs and you can therefore call Win32 API functions. Such as GetKeyState. Here's a script which gets the keystates for caps lock, num lock and scroll lock:
Let>VK_CAPITAL=20
Let>VK_NUMLOCK=144
Let>VK_SCROLL=145
LibFunc>User32,GetKeyState,CAP_STATE,VK_CAPITAL
LibFunc>User32,GetKeyState,NUM_STATE,VK_NUMLOCK
LibFunc>User32,GetKeyState,SCR_STATE,VK_SCROLL
CAP_STATE would equal 1 if Caps Lock is on, 0 if not. etc.
Let>VK_CAPITAL=20
Let>VK_NUMLOCK=144
Let>VK_SCROLL=145
LibFunc>User32,GetKeyState,CAP_STATE,VK_CAPITAL
LibFunc>User32,GetKeyState,NUM_STATE,VK_NUMLOCK
LibFunc>User32,GetKeyState,SCR_STATE,VK_SCROLL
CAP_STATE would equal 1 if Caps Lock is on, 0 if not. etc.
MJT Net Support
[email protected]
[email protected]
Works great, thanks!!!
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

Is there a way to reset the key state without having to to use sen>P in the script bellow? I would like a looping script go to a pause loops when the P key is pressed without it sending the letter P. Yes, I know I could use a pause button in the dialog but it's not possible with some of my scripts because of the speed.
Thanks for any help on this.
Dialog>Dialog1
Caption=
Width=123
Height=59
Top=168
Left=327
Max=0
Min=0
Close=1
Resize=0
Label=.,8,0,true
EndDialog>Dialog1
///80=P Key
lib>User32,GetKeyState,KEY_STATE,80
if>KEY_STATE=1
sen>P
endif
show>Dialog1
label>loop
wait>.0005
gda>Dialog1,r
if>r=2,2
lib>User32,GetKeyState,KEY_STATE,80
if>KEY_STATE=1
let>KEY_STATE=0
let>Dialog1.msLabel0= Key Activated
rda>Dialog1
wait>.3
let>Dialog1.msLabel0=
rda>Dialog1
wait>.3
goto>loop
endif
let>Dialog1.msLabel0=%CRLF% Key Not Activated
goto>loop
label>2
Thanks for any help on this.
Dialog>Dialog1
Caption=
Width=123
Height=59
Top=168
Left=327
Max=0
Min=0
Close=1
Resize=0
Label=.,8,0,true
EndDialog>Dialog1
///80=P Key
lib>User32,GetKeyState,KEY_STATE,80
if>KEY_STATE=1
sen>P
endif
show>Dialog1
label>loop
wait>.0005
gda>Dialog1,r
if>r=2,2
lib>User32,GetKeyState,KEY_STATE,80
if>KEY_STATE=1
let>KEY_STATE=0
let>Dialog1.msLabel0= Key Activated
rda>Dialog1
wait>.3
let>Dialog1.msLabel0=
rda>Dialog1
wait>.3
goto>loop
endif
let>Dialog1.msLabel0=%CRLF% Key Not Activated
goto>loop
label>2