Package: getkey
See it at:pypiGitHubVERSIONDOWNLOADSTESTSCOVERAGELibrary to easily read single chars and key strokes.Quick Startfromgetkeyimportgetkey,keyskey=getkey()ifkey==keys.UP:...# Handle the UP keyelifkey==keys.DOWN:...# Handle the DOWN keyelifkey=='a':...# Handle the `a` keyelifkey=='Y':...# Handle `shift-y`else:# Handle other text charactersbuffer+=keyprint(buffer)HistoryThis library seems to have started as a gist by Danny Yoo & made the rounds in
various versions until Miguel Ángel García turned it into a portable package
for theirpython-inquirerproject.  Then K.C.Saff forked it & smashed it
into this new form for their own command line input library.PhilosophyKeys will be returned as strings representing the received key codes, however
as some keys may have multiple possible codes on a platform, the key code will
be canonicalized so you can testkey == keys.UPinstead ofkey in keys.UP. This means non-control keys will be returned just as
the text they represent, and you can just as easily testkey == 'a'to
see if the user presseda.In addition, by default we will throwKeyboardInterruptforCtrl-Cwhich would otherwise be suppressed.  However, it is possible
to disable this if you wish:fromgetkeyimportplaformmy_platform=platform(interrupts={})my_getkey=my_platform.getkeyNowmy_getkeywill be a function returning keys that won’t throw onCtrl-C. Warning!  This may make it difficult to exit a running script.PlansThis library will not hit 1.0 until we can verify it works correctly with
unicode & international keyboards.  This is not yet tested.DocumentationInstallationpip install getkeyThegetkeylibrary is compatible with python 2.7, and 3.2+.UsageUsage example:fromgetkeyimportgetkey,keyskey=getkey()ifkey==keys.UP:...# Handle the UP keyelifkey==keys.DOWN:...# Handle the DOWN key...# Handle all other desired control keyselse:# Handle text charactersbuffer+=keyprint(buffer)Please consulttools/keys.txtfor a full list of key names available on
different platforms, ortools/controls.txtfor the abridged version
just containing control (normally non-printing) characters.APIThere is one primary method:getkey(blocking=True)Reads the next key-stroke fromstdin, returning it as an string.A key-stroke can have:1 character for normal keys: ‘a’, ‘z’, ‘9’…1 character for certain control combinations: ‘x01’ as Ctrl-A, for examplemore for other control keys (system dependent, but with portable names)checktools/keys.txtfor keys available on different systems.Interpreting the keycode response is made easier with thekeysobject:keysContains portable names for keys, so thatkeys.UPwill mean the up
key on both Linux or Windows, even though the actual key codes are
different.Because the list of key names is generated dynamically, please consulttools/keys.txtfor a full list of key names.  It is not necessary to
use key names for single characters: if the user pushesathe key returned
is very portably just that single characteraitself.keys.name(code)Returns the canonical name of the key which yields this key code on this
platform.  One key code may have multiple aliases, but only the canonical
name will be returned.  The canonical names are marked with an
asterisk intools/keys.txt.OS SupportThis library has been tested on both Mac & Windows, & the Mac keys should work
much the same on Linux.  If planning to use more esoteric control keys,
please verify compatibility by checkingHow to contributeYou can download the code, make some changes with their tests, and make a
pull-request.In order to develop or running the tests, you can do:Clone the repository.gitclonehttps://github.com/kcsaff/getkey.gitCreate a virtual environment:virtualenvvenvEnter in the virtual environmentsourcevenv/bin/activateInstall dependenciespipinstall-rrequirements.txt-rrequirements-dev.txtRun testsmakePlease,Execute the tests before any pull-request. This will avoid invalid builds.LicenseCopyright (c) 2014, 2015 Miguel Ángel García (@magmax9).Copyright (c) 2016 K.C.Saff (@kcsaff)Based on previous work on gistgetch()-like unbuffered character reading from stdin on both Windows and Unix (Python recipe), started byDanny Yoo.Licensed underthe MIT license.