#πŸ”’ Help detecting key releases in tkinter.

39 messages Β· Page 1 of 1 (latest)

stable cipher
#
from tkinter import *
from tkinter import ttk

def forward(event):
    print("forward")

def stop(event):
    print("stop")

root = Tk()

root.bind("<w>", forward)
root.bind("<KeyRelease-w>", stop)

root.mainloop()

Output when w is held:

forward
Stop
forward
Stop
forward
Stop
forward
Stop
forward
Stop

How do i make it do something like this?

forward
Stop

Or this:

forward
forward
forward
forward
forward
Stop

Im on linux if that helps.

gritty pendantBOT
#

@stable cipher

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hallow owl
stable cipher
#

Turning off key repeat seems to fix it, but i dont find this to be an ideal fix, the problem is that key repeat seems to also release the key before triggering it again.

wide onyx
#

what's your goal with this? If you're trying to detect if the key is held, you need some sort of function called on a timer

stable cipher
#

I dont understand.

wide onyx
#

What are you ultimately trying to do?

hallow owl
stable cipher
wide onyx
#

how many times should it call when pressed?

#

just once?

stable cipher
wide onyx
#
forward
forward
forward
forward
forward
Stop
#

you showed this though

stable cipher
#

oh

wide onyx
#

wouldn't that imply it should call multiple times if held?

stable cipher
#

wait your right

stable cipher
stable cipher
wide onyx
#

That's why I'm trying to ask what exactly you're trying to do

stable cipher
#

yeah sorry

#

ether one works

wide onyx
#

Checking if a key is being held requires a timer

#

What are you trying to do with these forward/stop functions?

stable cipher
#

send a signal over a socket to a robot

wide onyx
#

I don't know much about sockets, but you could create some sort of function that gets called on a "tick", like a frame update you'd have on a game

#

so let's say this function gets called once per second (or more depending on needs)

#

You can track which keys are being held by storing them in something like a set, and then each tick, just check which buttons are being held by seeing what's in the set

stable cipher
#

hmm

#

i think that would be unnecessary

#

but it gave me an idea

wide onyx
#
import tkinter as tk

def tick():
    pressed_label['text'] = ' '.join(pressed)
    root.after(1000, tick)

def add_pressed(event):
    pressed.add(event.keysym)

def remove_pressed(event):
    pressed.discard(event.keysym)


pressed = set()
root = tk.Tk()
root.bind_all('<KeyPress>', add_pressed)
root.bind_all('<KeyRelease>', remove_pressed)

pressed_label = tk.Label(font=('consolas', 16))
pressed_label.pack()

root.after(1000, tick)
root.mainloop()

#

here's a basic implementation you can test

stable cipher
#

I think i will take a different approach but thank you :D

#

!close

gritty pendantBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.