Discussion:
how can I detect if the shift key is currently down (on windows) (twapi?)
(too old to reply)
et99
2024-07-28 00:33:47 UTC
Permalink
I want to launch a windows batch job, which then runs a tcl program (passing in the file names dnd'd onto the batch file).

I would like to also be able to detect if the shift key is currently down in the tcl program. I can't use a normal key binding since the shift key will be pressed (and held) down before the tcl program is launched.

I asked chatGPT how this can be done, and it said:

#include <windows.h>

bool IsShiftKeyDown() {
return (GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0;
}

when I asked how to do this with tcl, it said to use a direct windows api call using twapi, and gave this (wrong) answer:

package require twapi

proc is_shift_down {} {
set result [twapi::call user32 GetAsyncKeyState VK_SHIFT]
return [expr ($result & 0x8000) != 0]
}


However, there is mentioned in the twapi docs that one can do a direct call, which this chatGPT example was attempting to do, but unfortunately, the docs says:

"The Windows API may be directly accessed by Tcl commands that map to Windows functions. This interface is not documented in the TWAPI documentation."

Assuming the above C code is correct, does anyone know how to use twapi to make that call?

Or... is there some way that tcl can do it directly?
Ashok
2024-08-01 04:47:27 UTC
Permalink
proc is_shift_down {} {expr {!!([twapi::GetAsyncKeyState 0x10] & 0x8000)}}
et99
2024-08-01 18:19:38 UTC
Permalink
proc is_shift_down {} {expr {!!([twapi::GetAsyncKeyState 0x10] & 0x8000)}}>
Thanks so much for that! I had tried your documentation website, but didn't find it there.

Ah, I think I see what tbd means now :)
et99
2024-08-03 19:11:30 UTC
Permalink
proc is_shift_down {} {expr {!!([twapi::GetAsyncKeyState 0x10] & 0x8000)}}>
I've expanded the above to allow for any key to be tested for up/down and also optionally return the status of the key having been recently pressed, as documented in the windows function GetAsyncKeyState. I've posted the code for this to the wiki here (at the bottom):

https://wiki.tcl-lang.org/page/KeySyms+on+platforms+other+than+X11
Harald Oehlmann
2024-08-05 06:38:31 UTC
Permalink
Post by et99
proc is_shift_down {} {expr {!!([twapi::GetAsyncKeyState 0x10] & 0x8000)}}>
I've expanded the above to allow for any key to be tested for up/down
and also optionally return the status of the key having been recently
pressed, as documented in the windows function GetAsyncKeyState. I've
https://wiki.tcl-lang.org/page/KeySyms+on+platforms+other+than+X11
Great work, thank you !
Maybe provide it to twapi using a ticket there...

https://sourceforge.net/p/twapi/bugs/

Tahnks,
Harald

Loading...