asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.24k forks source link

Linux F13 does not work #146

Open Keridos opened 7 years ago

Keridos commented 7 years ago

Getting errors when trying to use keyDown('f13') or keyUp('f13') on my linux system. v0.9.35 on python 3.6


down:
X protocol error:
<class 'Xlib.error.BadValue'>: code = 2, resource_id = 0, sequence_number = 10, major_opcode = 132, minor_opcode = 2

up:
X protocol error:
<class 'Xlib.error.BadValue'>: code = 2, resource_id = 0, sequence_number = 13, major_opcode = 132, minor_opcode = 2
kartikeya100 commented 6 years ago

@asweigart @hugoesb I think i found something here https://social.msdn.microsoft.com/Forums/vstudio/en-US/cd0ad6b9-77a4-434d-8ed7-722ae21a39f1/keyboard-shortcuts?forum=vbgeneral https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx

vatre commented 6 years ago

I have the same error with python 3.6.4, pyautogui 0.9.36 on an Ubuntu Budgie 18.04.1 laptop and in a virtual box in an Arch install with X11 and XFCE4 (python 3.7.0 and pyautogui 0.9.38).

Running:

import pyautogui
pyautogui.keyDown("f15")

Raises:

X protocol error:
<class 'Xlib.error.BadValue'>: code = 2, resource_id = 0, sequence_number = 14, major_opcode = 132, minor_opcode = 2

Anyone has any idea how to solve this ? For context, I'm trying to replicate the caffeine utility which sends F15 keypress every 59 seconds to prevent lock up of the computer.

Thanks

alaestor commented 4 years ago

Running Manjaro XFCE; I was having a similar problem but discovered it was caused by the default configuration of my xmodmap. The F13..F24 keys are supposed to have keycodes ranging from 191..202. You can run xmodmap -pke in a terminal to see current bindings; some defaults tend to neglect >F12. Any keysym that doesn't have a corresponding X keycode will cause that BadValue error because keysym_to_keycode() (or rather, the underlying C XKeysymToKeycode()) will return 0.

pyautogui's code has lines like

_display.keysym_to_keycode(Xlib.XK.string_to_keysym('F13'))

In those statements, Xlib.XK.string_to_keysym('F13') returns the proper keysym but _display.keysym_to_keycode returns 0 due to X not having the keysym in it's table.

I'd recommend that pyautogui's x11 script check for invalid 0 keycodes before passing them to fake_input

For anyone having this issue, the Arch wiki's article on xmodmap goes into how you can change them. I corrected the problem by changing the following, and everything has been working fine.

keycode 191 = F13 F13 F13 F13 F13 F13
keycode 192 = F14 F14 F14 F14 F14 F14
keycode 193 = F15 F15 F15 F15 F15 F15
keycode 194 = F16 F16 F16 F16 F16 F16
keycode 195 = F17 F17 F17 F17 F17 F17
keycode 196 = F18 F18 F18 F18 F18 F18
keycode 197 = F19 F19 F19 F19 F19 F19
keycode 198 = F20 F20 F20 F20 F20 F20
keycode 199 = F21 F21 F21 F21 F21 F21
keycode 200 = F22 F22 F22 F22 F22 F22
keycode 201 = F23 F23 F23 F23 F23 F23
keycode 202 = F24 F24 F24 F24 F24 F24