First of all, you forgot to close the strings on lines 4 and 6.
But your main issue is that you call keyboard.read_key() once for each arm of your if statement. This makes your code wait for a keypress, check if it's 'enter', then wait for another keypress, check if it's 'q', and so on.
What you should do is save the key to a variable, then check the variable. It should look something like this:
import keyboard
while True:
key = keyboard.read_key()
if key == 'enter':
print('Enter is pressed')
if key == 'q':
print('Quitting the program')
break
if key == 's':
print('Skiping the things')
Answer from Jason on Stack Overflow
» pip install keyboard
How to detect keypress in python using keyboard module? - Stack Overflow
Is there a doc for the keyboard library?
Using keyboard in my program
Cross platform keyboard input
Videos
First of all, you forgot to close the strings on lines 4 and 6.
But your main issue is that you call keyboard.read_key() once for each arm of your if statement. This makes your code wait for a keypress, check if it's 'enter', then wait for another keypress, check if it's 'q', and so on.
What you should do is save the key to a variable, then check the variable. It should look something like this:
import keyboard
while True:
key = keyboard.read_key()
if key == 'enter':
print('Enter is pressed')
if key == 'q':
print('Quitting the program')
break
if key == 's':
print('Skiping the things')
As per Keyboard documentation:
Other applications, such as some games, may register hooks that swallow all key events. In this case keyboard will be unable to report events.
One way to solve your problem with keyboard module is keyboard.wait('key')
# Blocks until you press esc
keyboard.wait('esc')
Something work around is as below:
import keyboard
keyboard.wait('enter')
print('Enter is pressed')
keyboard.wait('q')
print('Quitting the program')
keyboard.wait('s')
print('Skiping the things')
I want to make a keylogger and learn more about the keyboard module. PyPI has a doc for it but I doesn't go in depth on what methods to input and what they mean.
I found the python standard library but the doc is not there
https://docs.python.org/3/library/index.html
I wanted to know where can I find the doc for it or where can I learn about the keyboard library?
It looks like the module takes Windows keys, will the keyboard library work with Mac as well and can I use the same key inputs when coding?
Also, I am beginner in python will a Keylogger be a good beginner project?
It can be done using ctypes:
import ctypes
from ctypes import wintypes
import time
user32 = ctypes.WinDLL('user32', use_last_error=True)
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
KEYEVENTF_SCANCODE = 0x0008
MAPVK_VK_TO_VSC = 0
# msdn.microsoft.com/en-us/library/dd375731
VK_TAB = 0x09
VK_MENU = 0x12
# C struct definitions
wintypes.ULONG_PTR = wintypes.WPARAM
class MOUSEINPUT(ctypes.Structure):
_fields_ = (("dx", wintypes.LONG),
("dy", wintypes.LONG),
("mouseData", wintypes.DWORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
class KEYBDINPUT(ctypes.Structure):
_fields_ = (("wVk", wintypes.WORD),
("wScan", wintypes.WORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
def __init__(self, *args, **kwds):
super(KEYBDINPUT, self).__init__(*args, **kwds)
# some programs use the scan code even if KEYEVENTF_SCANCODE
# isn't set in dwFflags, so attempt to map the correct code.
if not self.dwFlags & KEYEVENTF_UNICODE:
self.wScan = user32.MapVirtualKeyExW(self.wVk,
MAPVK_VK_TO_VSC, 0)
class HARDWAREINPUT(ctypes.Structure):
_fields_ = (("uMsg", wintypes.DWORD),
("wParamL", wintypes.WORD),
("wParamH", wintypes.WORD))
class INPUT(ctypes.Structure):
class _INPUT(ctypes.Union):
_fields_ = (("ki", KEYBDINPUT),
("mi", MOUSEINPUT),
("hi", HARDWAREINPUT))
_anonymous_ = ("_input",)
_fields_ = (("type", wintypes.DWORD),
("_input", _INPUT))
LPINPUT = ctypes.POINTER(INPUT)
def _check_count(result, func, args):
if result == 0:
raise ctypes.WinError(ctypes.get_last_error())
return args
user32.SendInput.errcheck = _check_count
user32.SendInput.argtypes = (wintypes.UINT, # nInputs
LPINPUT, # pInputs
ctypes.c_int) # cbSize
# Functions
def PressKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode,
dwFlags=KEYEVENTF_KEYUP))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def AltTab():
"""Press Alt+Tab and hold Alt key for 2 seconds
in order to see the overlay.
"""
PressKey(VK_MENU) # Alt
PressKey(VK_TAB) # Tab
ReleaseKey(VK_TAB) # Tab~
time.sleep(2)
ReleaseKey(VK_MENU) # Alt~
if __name__ == "__main__":
AltTab()
hexKeyCode is the virtual keyboard mapping as defined by the Windows API. The list of codes is available on MSDN: Virtual-Key Codes (Windows)
For both python3 and python2 you can use pyautogui (pip install pyautogui)
from pyautogui import press, typewrite, hotkey
press('a')
typewrite('quick brown fox')
hotkey('ctrl', 'w')
It's also crossplatform with Windows, OSX, and Ubuntu LTS.
So recently i decided to learn how to code on python, and i tried downloading the keyboard module.
I went down to my cmd, typed "pip install keyboard", succesfull.
Tried to use it in python using "import keyboard", not so successfull.
Got this error messsage:
" import keyboard
ModuleNotFoundError: No module named 'keyboard'"
I tried uninstalling keyboard from my cmd, rebooting my pc, using the "pip3 install keyboard", nothing worked. I read somewhere that it might be due to an interpreter problem, but i don't knoow how to fix it.
Hope some of y'all could help me :)