Ask coding questions

← Back to all posts
Detect Custom Keys
RYANTADIPARTHI (6019)

I want to know if there is a module or a way to detect keys and perform some action when clicked in the python console.

So for example, if I clicked shift + a, it should give something like text.

So I should be able to make custom shortcuts. I tried many modules, but none of them seem to be working on replit.

Please let me know if you know of a module or a way to do this in the python console.

Thanks!

Comments
hotnewtop
Aivoybia (24)

For Control + C,

import signal

def catch_ctrl_C(sig,frame):
      # Code here
  signal.signal(signal.SIGINT, catch_ctrl_C)

Hope this helps.

Have a great day :D

RYANTADIPARTHI (6019)

@MEadphonesmead hi, i don't know how to make this work, but if this works, can you show how to do ctrl + ., and when i click it, please make it show "success.". Could you do that? Thanks!

Aivoybia (24)

@RYANTADIPARTHI i dont think you can do that with period because period is used a lot in every language, so you cant do it with "."

Aivoybia (24)

@RYANTADIPARTHI oh and for the control c, the signal.signal l one is indented, while it is not supposed to

RYANTADIPARTHI (6019)

@MEadphonesmead well, i need to use the . for my task. Can't you make the . a string? Then it doesn't affect anything. But please let me know if you can do it. Using this, or anything else. Thanks!

Aivoybia (24)

@RYANTADIPARTHI ok so when i test it on spotlight page or the repl, the shortcut window pops up, so I don't know if it works

RYANTADIPARTHI (6019)

@Aivoybia oh. how about you click on the console, and then try it?

Aivoybia (24)

@RYANTADIPARTHI also if i put the . in quotes it has a syntax error idk why

Aivoybia (24)

@RYANTADIPARTHI ill invite you to a repl and then delete it later so we don't need to keep refreshing the page

RYANTADIPARTHI (6019)

@Aivoybia sorry, i didn't see your comments, I was away from my computer, but I'm good now. Also, I don think the ctrl + . problem will occur for me, since i'm not doing it in the console, i'm doing it in a webpage. So, I think if you just give me the correct code, it will work on the webpage. Could you do that? Thanks!

Aivoybia (24)

@RYANTADIPARTHI

import signal


# if you want to have custom, control is ctrl (on the keyboard), and for letters, it must be capital. Im pretty sure the abbreaviation for period is prd, idk
# wherever it says catch_ctrl_period, change the "period" to somethin else if this doesnt work
def catch_ctrl_period(sig,frame):
  print("Kewl")
signal.signal(signal.SIGINT, catch_ctrl_period)
RYANTADIPARTHI (6019)

@Aivoybia i don't think naming it period works like how you did for C. since you're putting the word period i guess It doesn't work. Do you know how to make it work? please let me know. Thanks!

Aivoybia (24)

@RYANTADIPARTHI ok i dont know, search up abbreviations for period and I'm sure something will work.

VulcanWM (2875)

This is Python right? Use the module getkey. The documentation is here: https://pypi.org/project/getkey/

RYANTADIPARTHI (6019)

@VulcanWM yes this is python, i specified it in the description already, but for getkey, I have a question. Can it handle keys like ctrl + shift + a or more like that?

VulcanWM (2875)

i dont think so but you can try it out @RYANTADIPARTHI

RYANTADIPARTHI (6019)

@VulcanWM it doesn't seem to be working. Do you have any other options?

Aivoybia (24)

@RYANTADIPARTHI uh yes i know for control c or something like that