Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
keyboard listener
#1
Hi,

I am looking for a module that will do the simplest of things:
An app starts, and it waits for the user to enter 1 keyboard key (=validated if need be).
The app registers it, does something, and then waits for the next 1 key.(GUI = Tkinter)

I pip installed keyboard and encountered some issues for which i wrote a workaround.
It works, but not to my satisfaction.

The question:
Before i start experimenting with other modules, what would anyone recommend as being the obvious choice,
for doing this.

thx,
Paul
Reply
#2
You're looking for a keyboard listener.
there are several here: https://pypi.org/search/?q=keyboard
This one looks promising (but haven't tried): https://pypi.org/project/keyboard-listener/
Reply
#3
OK, thanks , I'll look into those.
This combined with "switch case" statement would be the ultimate.
I seem to have read that it's about to happen. Smile

Paul
Reply
#4
If you mean this article: https://towardsdatascience.com/switch-ca...caf7b2bfd3
I also read that, but it doesn't make sense as version 3.9.3 is current version and the next major release will be 4.0 no mention that I have found of a case statement, other than the only official information of PEP 3101 (from 2006) https://www.python.org/dev/peps/pep-3103/ which was rejected. Article states that the case statement will appear in release 3.1.0 ...

A dictionary whose keys are the 'keyboard keypress', and whose value is a function pointer to be executed when the key is pressed would be a much more efficient and easier to program solution.
Reply
#5
The article seems to be talking about release 3.10...
But i do not have inside info... Rolleyes

Anyway we had a lengthy thread here a couple of months ago,
about using dictionaries instead of switch case, which is quite OK.

I made the point then that "Select or swith Case" statements where already
commonplace decades ago elsewhere. In a lot of cases a dictionary will do the trick though.

Paul
Reply
#6
I am an old C (Bell labs, early 1980's) and C++ and used switch case often.
Python dictionary is much more robust IMHO.
I'm an old timer, first official programming job was in 1968.
Slowing down, but still at it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't stop keyboard listener to grab chars typed inside CTk window Valjean 9 1,309 Sep-25-2023, 08:07 PM
Last Post: deanhystad
  cx_Oracle.DatabaseError: ORA-12541: TNS:no listener ARV 2 11,053 Oct-03-2019, 12:53 PM
Last Post: ARV

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020