-1

For normal alphanumeric keys it can be checked as fallow:

def on_press(key):
    try:
        if key.char == "a":
            # do sth

but what about special keys like f1 or Ctrl ?

mike
  • 45
  • 5
  • 2
    have you tried, hm... checking documentation maybe? – Marcin Orlowski Aug 08 '20 at 09:14
  • @Marcin Orlowski yes but i cant figure it out . it something related to Key class .but i just started python and i can't understand how to solve it.i really appreciate if you could help me. – mike Aug 08 '20 at 09:16

1 Answers1

0

According to the documentation, the function keys F1 to F20 are defined and many other keys like CTRL can also be checked. See the documentation for the internal names of the keys, but for F1 it should work as follows:

from pynput.keyboard import Key

def on_press(key):
    if key == Key.f1:
        # do something
mrizp
  • 91
  • 1
  • 6