question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Whats the best way to execute some code after certain keys have been pressed?

See original GitHub issue

First of all thanks for this! I am relatively new to python and i just discovered “keyboard” and i think it’s awesome.

I am struggling to find elegant way how to make some code to be executed if certain keys have been pressed (not one) for example… if the user pressed f1, w, (do something) or… shift+`, w, e, (do something)

Would you use the record feature here or the world listener or something else?

Thank you

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
Qwertronixcommented, Nov 29, 2018

Indeed, this is how you would go about it. Example:

import keyboard

def myFunc():
    print('It works! You hit F1 and then typed "write" and triggered this function.')

keyboard.add_hotkey('F1, w, r, i, t, e', myFunc)
keyboard.wait()
1reaction
Farm-Artcommented, Nov 25, 2018

IIRC you can use commas in the string to listen for consecutive presses, e.g. “shift+e, r, f”

Read more comments on GitHub >

github_iconTop Results From Across the Web

JS function when keyboard key is pressed? - Stack Overflow
Part 1: Where to put the scriptblock? To capture over the entire page, like as a page-help-function (maybe you want to capture F1?)...
Read more >
Keyboard: keydown and keyup - The Modern JavaScript Tutorial
Pressing a key always generates a keyboard event, be it symbol keys or special keys like Shift or Ctrl and so on. The...
Read more >
JavaScript Keycode List – Keypress Event Key Codes for ...
The keydown event has the maximum coverage of keys to produce the contextual information. The keypress event works only for a subset of...
Read more >
How to Detect a Sequence of Keystrokes in JavaScript
To save the sequence of entered keys we need to add a variable that will store that sequence and which we can update...
Read more >
Use JavaScript to Detect What Key the User Pressed
In some apps and games it may be necessary to enable certain ... The event listener logs the code, which has been converted...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found