0

Suppose you have this code.

def test(widget):
    print(keyboard.is_pressed("shift"))

t=tkinter.Tk()
b=tkinter.scrolledtext.ScrolledText()
b.bind("<Return>", test)
b.pack()
t.mainloop()

Just on the very first try of holding shift and pressing enter, it prints False but after that it works fine (the first time this event is triggered, if shift is held down, it is not detected, but after that it works).

martineau
  • 119,623
  • 25
  • 170
  • 301
Hormoz
  • 291
  • 1
  • 2
  • 7
  • It's quite likely that the `keyboard` module doesn't play well with `tkinter` since both handle relatively low-level user input events. You can do what you're trying to accomplish here with just `tkinter`, btw. – martineau Jun 20 '21 at 22:58
  • With just Tkinter? How? I am not trying to detect both pressing shift and enter, I am trying to prevent something from happening if shift is pressed with enter, so I can't just add shift to the event list. – Hormoz Jun 20 '21 at 23:00
  • That's a different question than what you're asking here — but the (very) short answer is by binding to other event types (or keys). – martineau Jun 20 '21 at 23:09
  • This might not work for this exact specific purpose. Since I want to prevent the binding from happening if shift is pressed (or at least a specific part of the function that's bound). Not making shift also trigger it. – Hormoz Jun 20 '21 at 23:25
  • You can bind to the one or both of the shift keys and track their state. – martineau Jun 20 '21 at 23:27
  • 1
    See [check if modifier key is pressed in tkinter](https://stackoverflow.com/questions/19861689/check-if-modifier-key-is-pressed-in-tkinter) for examples. – martineau Jun 20 '21 at 23:36
  • This can work to some extent (if I bind the key-release and key-press to track the state, and store it in a variable for example). The problem is Tkinter has no way of detecting whether a key is pressed or not beyond that. For example if shift is pressed then the window goes out of focus and then shift is released, Tkinter wouldn't be able to tell that shift is not pressed when the window comes into focus again. – Hormoz Jun 20 '21 at 23:37
  • You can also detect windows getting and losing focus. – martineau Jun 20 '21 at 23:39
  • @Hormoz Try using this: `print(bool(event.state & 0x0001))`. That will check if the shift button is pressed when the event was fired. – TheLizzard Jun 20 '21 at 23:41

1 Answers1

1

You can use what @martineau pointed out in the comments check if modifier key is pressed in tkinter like this:

import tkinter as tk
from tkinter.scrolledtext import ScrolledText

def test(event):
    # If you want to get the widget that fired the event, use this:
    widget = event.widget

    # Check if the shift key is pressed when the event is fired
    shift_pressed = bool(event.state & 0x0001)
    if shift_pressed:
        ...
    else:
        print("Shift isn't pressed.")

root = tk.Tk()
text_widget = ScrolledText(root)
text_widget.bind("<Return>", test)
text_widget.pack()
root.mainloop()

event.state is an integer that holds flags for different keys combinations that are pressed when the event is fired. The & 0x0001 checks to see if the shift key is pressed when the event is fired.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31