The Untold Secret of Python getch Library

In this article, we will learn about the python getch module. While building the user interface for the console, we sometimes want to get some feature of pausing the screen or reading a text without showing it out on the console; This module helps us in those scenarios. So, let’s start learning this module.

Usage of Python getch library

While coding, we are often stuck in situations where we need to perform some action when the user hits any button from the keyboard or may enter some value. Based on different conditions, python getch is used in different ways. So, getch module does single-char input by providing wrappers for the “conio.h” library functions. However, if conio.h is missing, it uses a stub-library using “terminos.h” to follow up the functionality.

[ Note:- Stub libraries are import libraries that export symbols but do not contain any code.]

The getch module contains two functions with almost the same functionalities with some differences. Those are “getch()” and “getche()”. They are used to reading single characters from CLI. The only difference is that getch() doesn’t show the input value on the screen while getche() does. The most general case in which getch() is used is while entering a password. However, sometimes we also want to hold the screen till the user hits something. We can also use it there. So before discussing examples on it, first see its installation.

Installing Python getch

So, It is essential to install it before importing it as it is not a built-in python library. To do that, we can use the following command.

pip install getch

Python getch on Unix and MacOS

Examples

Let’s understand each function of getch library using examples to get hands-on to them. But, before that, we need to import it using the following command.

from getch import getch, getche

Learn to import classes from other resources here.

Example 1

from getch import getche, getch

a = getch()
print(a)
Input:(Invisible on the console)

4 
Output:

4

Example 4

from getch import getche, getch

a = getche()
print(a)
Input:(Visible on the console)

3
Output:

3

Python getch on windows

While the above discussed getch module is only compatible with Linux OS, we can also use it in windows by “msvcrt module”. getch() function is also available there for use, and it functions the same way getch module does. Let’s discuss an example of it.

Example 1

from msvcrt import getche, getch

a = getch()
print(a)

a = getche()
print(a)

Output

Input: (Invisible on the console)
3

Output:
3

Input:(Visible on the console)
4

Output:
4

Python getch for arrow keys

However, we will be able to use characters as input for getch() and getche() method but, we can’t use arrow keys in the same way. The reason behind that definition of those arrow keys is different for the console, and hence we have to define it the way they are defined for the console. Without making it more complex, let’s try to understand it through an example,

def get_key():
    first_char = gh.getch()
    if first_char == '\x1b':
        return {'[A': 'up', '[B': 'down', '[C': 'right', '[D': 'left'}[gh.getch() + gh.getch()]
    else:
        return first_char


key = ''
while key != 'q':
    key = get_key()
    print(key)

In the above example, we used the exact meaning of the escape sequence to understand accordingly.

Equivalent libraries to Python getch

However, it can be used, but several libraries can function the same way. Some of them are independent of OS, unlike getch, or some of them provide non-blocking also. Let’s see them.

LibraryDescription
py-getchProvides pause() and pause_exit() as well.
getchlibOS Independent, Blocking and non-blocking keypress reading
pygetchSame as py-getch but platform-independent.

What does Msvcrt getch return?

It is as same as getch.getch() method, just the difference is that it is used for windows console and getch.getch() is used for Unix Console.

What is getch in C?

It is the predefined non-standard function defined in conio.h header file. It is used to hold the output screen until the user presses any key.

Conclusion

So, In this article, we learned about the python getch module. We also looked at its core functionality of reading a character only using getch() and getche() methods. We also looked at the differences between both of them.

I hope this article has helped you. Thank you.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments