0

i´ve been working on a script that displays a keyboard when I press a specific button on a controller with tkinter . The idea behind it was that I wanted to use my computer without a mouse or keyboard. The Keyboard already works, however if I press a key on the generated keyboard the response will obviously go to the keyboard window, which could only be used to copy the written string and paste it via the button later. But I want the keyboard window to send the response directly to the input field. Is there any way you can achieve this? Does anyone know if there are libraries that could do that? (best would be a method for tkinter, which is not OS specific)

Im using Python 3.8 on Mac Os high sierra

The code looks something like this

def b_1_response():
    keyboard.press("1")
    keyboard.release("1")

def createKeyboard(keymode):

    window = tk.Tk()
    window.overrideredirect(1)#removes the title bar
    window.overrideredirect(0)#need to do this because mac os is stupid sometimes :/
    screen_width = window.winfo_screenwidth()
    screen_height = window.winfo_screenheight()
    y = screen_height - keyboard_height
    x = (screen_width - keyboard_width)/2
    settings = str(keyboard_width) + "x" + str(keyboard_height) + "+" + str(x) + "+" + str(y)
    window.configure(bg="#1C1C1C")
    window.geometry(settings)




    button_1 = Button(window, text=keymode[0], bg="black", fg="white", command=b_1_response, height=90, width=90)
    window.mainloop()

I haven´t really tried anything because I couldn´t find similar questions to this in python, but heres a similar question in java: Sending a keyboard event from java to any application (on-screen-keyboard) Im also not very experienced with programming and this is my first questions so please don´t be too harsh :D

  • I don't think you can use tkinter to generate OS events like keyboard presses. Also can you please fix your code's indentation? – TheLizzard Apr 01 '21 at 14:35
  • The keyboard calls a function that presses a key with pynput, but I want the output to be written on a text field directly – Nippelfrei Apr 01 '21 at 14:41
  • What if you used `window.withdraw()` before sending the keypress then `window.deiconify()` after? It should hide the window before the press and show it aster the key press is sent – TheLizzard Apr 01 '21 at 14:45
  • That works, but I had to click on the input field again and add a timer between window.withdraw and window.deiconify(). Is there some way of bringing the keyboard to the front without actually focusing on the keyboard? Thanks for the help btw :D didn´t expected a reply this quickly – Nippelfrei Apr 01 '21 at 14:55
  • What if you add `window.update()` just after `window.withdraw()`? That should remove the need for a timer - might not work. – TheLizzard Apr 01 '21 at 14:58
  • I only added the timer to click on input field so the key sent would actually do something but window.update looks bad because the keyboard disappers for a moment – Nippelfrei Apr 01 '21 at 15:03
  • Asking for library recommendations is off topic for Stackoverflow. – Bryan Oakley Apr 01 '21 at 15:14
  • Why? I thought this platform is made for poeple, who need answers for their problems and if a library is able to solve my problem, then it is a good idea to recommend me a library :/ – Nippelfrei Apr 01 '21 at 15:32
  • I would say that python 2.7 could be part of the issue since it no more is supported, so I suggest updating that but about the no focus on top of all windows I can suggest this: `root.wm_attributes("-topmost", 1)` where root is the Tk() class instance, this will keep the window on top at all times (unless iconified or deleted I guess) – Matiiss Apr 01 '21 at 17:45
  • I updated the version to 3.8 (thought this will cause problems but it didn´t yay). root.wm_attributes("-topmost", 1) is a very good addition but im still not able to send the inputs directly to the window with the input field. – Nippelfrei Apr 01 '21 at 20:18

0 Answers0