Mailing List Archive

Re: [tkinter][Windows]Unexpected state report for the tab key
On 10/24/21, Stefan Ram <ram@zedat.fu-berlin.de> wrote:
> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>tab_down_hllDll.GetKeyState(tab_down_VK_TAB) & 0b1000000000000000
>
> In the meantime, I read about "GetAsyncKeyState". I thought that
> this would solve my problem, but no.

Odd. It works for me in the classic console and the Windows Terminal
pseudoconsole. The tab key's up/down state is updated immediately.

import ctypes
import msvcrt
import time

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
user32 = ctypes.WinDLL('user32', use_last_error=True)

VK_TAB = 0x09

def tab_down():
return bool(user32.GetAsyncKeyState(VK_TAB) & 0x8000)

def flush_console_input():
"""Flush the input buffer of the process (pseudo)console."""
try:
with open('conin$') as f:
h = msvcrt.get_osfhandle(f.fileno())
kernel32.FlushConsoleInputBuffer(h)
except OSError:
pass

def test():
for i in range(100):
print('tab down:', tab_down())
time.sleep(0.1)
flush_console_input()

test()

Flushing the console input buffer isn't necessary. I just prefer it
instead of leaving the key presses in the buffer.

Usually a GUI wants GetKeyState() instead, which is synchronized with
input messages in the thread's message queue as they're processed. But
GetAsyncKeyState() should do what you want, if you don't need
synchronization with input messages.
--
https://mail.python.org/mailman/listinfo/python-list
Re: [tkinter][Windows]Unexpected state report for the tab key [ In reply to ]
On 25/10/2021 02:57, Stefan Ram wrote:
> GetKeyState still returns that the tab key
> is pressed after the tab key already has been released.
> Well, how then am I going to make my slide show stop the
> moment the key is being released?
>
>
Does tkinter allow you to trap KeyUp (and KeyDown) events?
(I'm not familiar with tkinter, but in wxpython you can.)
And if so, does trapping KeyUp solve the problem?
Best wishes
Rob Cliffe

--
https://mail.python.org/mailman/listinfo/python-list