Ask coding questions

← Back to all posts
How do I bind a key to run a section of code?
Jrb12 (0)

So I've been working on a project, and wondering if there was a way that I could bind a key so that whenever you press that key (i.e., 7 or 9) that it would run a certain section of code. Is that possible?

Answered by InvisibleOne (2986) [earned 5 cycles]
View Answer
Jrb12 (0)
InvisibleOne (2986)

Yeah, it looks like your using getch, so you would need to do something like this:

from getch import getch

def myFunction():
  #run this code

while True:
  i = getch()
  if i == '9':
Jrb12 (0)

Thanks! So what code do I put in? Just anything I want it to execute? @InvisibleOne

InvisibleOne (2986)

Yeah, a function is just a block of code, and then you can call that function and execute that code. @Jrb12

InvisibleOne (2986)

And by looking at your code, I can see that you've got most of it right, but on line 19 it should be: if debut == "BossFight":

Jrb12 (0)

@InvisibleOne K, thanks! Will be experimenting a bit

InvisibleOne (2986)

np, let me know if you have anything I might be able to help you with. @Jrb12