-1

I want to detect if Ctrl + X is clicked so that I can exit my Python application. I don't know how to do it? Please help.

alyssaeliyah
  • 2,214
  • 6
  • 33
  • 80

2 Answers2

1

Have you thought about using a function like the ones discussed here? Python read a single character from the user

Or maybe you could use curses.

Either way you would just need to find the key code for ctrl-X, it's 24.

onlynx76
  • 11
  • 3
0

The simple and the best option is to use the module keyboard. Install it using pip install keyboard.
Use the following code at the start of your code:

import keyboard as k
k.add_hotkey("ctrl+x",lambda: quit())

#Your code....

Well, it works easily, but, it will read keys from the whole windows. For example the program is running and you are using notepad currently and you pressed ctrl+x , then the python program will close too.

Nouman
  • 6,947
  • 7
  • 32
  • 60