0

I'm looking for a simple way to detect a keypress inside a loop without importing something like pygame, something along the lines of

pressed('a') #returns True if key 'a' pressed, False otherwise

I'm aware of this Keypress detection thread, but unfortunately when I run the recommended code or try anything using keyboard.is_pressed(), I get

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\myname\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
self.run()
  File "C:\Users\myname\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
  File "C:\Users\myname\AppData\Roaming\Python\Python37\site-packages\keyboard\__init__.py", line 292, in listen
_os_keyboard.listen(self.direct_callback)
  File "C:\Users\myname\AppData\Roaming\Python\Python37\site-packages\keyboard\_winkeyboard.py", line 560, in listen
prepare_intercept(callback)
  File "C:\Users\myname\AppData\Roaming\Python\Python37\site-packages\keyboard\_winkeyboard.py", line 553, in prepare_intercept
keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_callback, GetModuleHandleW(NULL), NULL)
ctypes.ArgumentError: argument 3: <class 'OverflowError'>: int too long to convert

(Pretty new to Python, so not sure what any of the above means)

keyboard.is_pressed() is my ideal solution, but I'm completely open to other equally simple ones too.

Evan C
  • 45
  • 1
  • 6
  • Python uses a low level keyboard hook? This makes me sad :( – AnilRedshift Jul 09 '18 at 22:20
  • What do you mean by "without importing something like pygame"? In what way is that different from importing something like `keyboard`? – abarnert Jul 09 '18 at 22:20
  • 1
    @AnilRedshift No, a third-party library uses a low-level keyboard hook. – abarnert Jul 09 '18 at 22:20
  • Anyway, how did you install keyboard, and which version do you have? (If you don't know how to answer that, `pip list` should show everything you have installed.) And, while we're at it: what Windows version, what Python version, how did you install Python (e.g., python.org installer vs. Anaconda vs. build and install from source vs. …)? – abarnert Jul 09 '18 at 22:23
  • Also, please give us a [mcve] that demonstrates the problem. From the traceback, it looks like you may be trying to use this from a background thread, but for all I know maybe the library just spawns a thread for itself on Windows and you're not doing anything like that. If we could see your example, it would be easier to know what to chase down. – abarnert Jul 09 '18 at 22:27
  • @abarnert I installed the latest version of keyboard via pycharm with pip. Running windows 10, using python 3.7. My example was meant to be the code in the approved answer of the link; didn’t want to copy all of that and make a super long post (correct me if I’m wrong, I thought that was reasonable, but I am new to this forum) – Evan C Jul 10 '18 at 02:12
  • @AnilRedshift how else would getting keyboard events happen? the OS almost always sits in-between hardware and software.. How do you think tkinter and pygame get keyboard input? – Aaron Jul 10 '18 at 16:34

2 Answers2

0

This is an open issue with python 3.7 on the github. I just tested the code given in your link with Python 3.6.4 (anaconda 64 bit) and keyboard-0.13.2, and it worked as expected. Python 3.7 literally just came out, so there are bound to be a few bugs.

Aaron
  • 10,133
  • 1
  • 24
  • 40
  • oh thanks, didn't even realize that python 3.7 was that new. – Evan C Jul 11 '18 at 02:55
  • @EvanC development versions have been available for a while ([3.7.0 alpha 1 released 2017-09-19](https://docs.python.org/3/whatsnew/changelog.html#python-3-7-0-alpha-1)) but the official release of 3.7 happened June 27th 2018. Just two weeks ago. – Aaron Jul 11 '18 at 18:10
0

I just wanted to point out this thread. Detect key press in python?

I'm fairly new myself and found that a simple loop to detect a keypress with keyboard.is_pressed() causes issues (when not breaking).

At worse, all keyboard functions would be delayed by it when complexity steps up. I also ran into other issues.

The third answer in that thread points out the keyboard.wait() function. I found it to be my favorite way to handle a simple keypress.

example

import keyboard
import time

while True:
    keyboard.wait("a")
    keyboard.press_and_release("z")
    time.sleep(0.3)

I still don't think I'm handling it perfectly, but this cleaned up all issues.