Thread from comp.lang.tcl (4 replies)

how can I detect if the shift key is currently down (on windows) (twapi?)
Posted by et99 <et99@rocketship1.me> 2 months ago

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?

Click on article to view all threads in comp.lang.tcl
Re: how can I detect if the shift key is currently down (on windows) (twapi?)
Posted by Ashok <apnmbx-public@yahoo.com> 2 months ago

proc is_shift_down {} {expr {!!([twapi::GetAsyncKeyState 0x10] & 0x8000)}}

Click on article to view all threads in comp.lang.tcl
Re: how can I detect if the shift key is currently down (on windows) (twapi?)
Posted by et99 <et99@rocketship1.me> 2 months ago

On 7/31/2024 9:47 PM, Ashok wrote:
> 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 :)











Click on article to view all threads in comp.lang.tcl
Re: how can I detect if the shift key is currently down (on windows) (twapi?)
Posted by et99 <et99@rocketship1.me> 2 months ago

On 8/1/2024 11:19 AM, et99 wrote:
> On 7/31/2024 9:47 PM, Ashok wrote:
>> 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



Click on article to view all threads in comp.lang.tcl
Re: how can I detect if the shift key is currently down (on windows) (twapi?)
Posted by Harald Oehlmann <wortkarg3@yahoo.com> 1 month 4 weeks ago

Am 03.08.2024 um 21:11 schrieb et99:
> On 8/1/2024 11:19 AM, et99 wrote:
>> On 7/31/2024 9:47 PM, Ashok wrote:
>>> 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
> 
> 
> 
Great work, thank you !
Maybe provide it to twapi using a ticket there...

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

Tahnks,
Harald

Click on article to view all threads in comp.lang.tcl