2

I have a Python module that listens for a key combination using pynput then, once it's pressed, it types a string into a text program.

It works great! Except...

In the example below, the user's key combo is set to shift + space. This makes a lot of sense and will likely be the most common chosen key command for Windows users running my program. The trouble is, while the shift key is held down it changes what pynput types. Instead of 01/20/2019, it will type )!/20/2019.

I need a way to disable the keyboard until pyautogui is finished typing the string. Thanks a lot for your help!!!

Bonus question: I can't seem to get a result when the key combination includes a ctrl key. Key.ctrl simply fails to ever trigger, whilst other keys work fine.

from pynput.keyboard import Key, Controller, Listener
import time

keyboard = Controller()

def insert():                                 # check line 1 of config file 
     keyboard.type('01/20/2019')

# The currently active modifiers
current = set()

def on_press(key):
    if key in COMBINATION:
        current.add(key)
        if all(k in current for k in COMBINATION):      # I don't know what this k in current for k shit is.
            current.remove(key)
            insert()                                # run insert

    if key == Key.esc:
        listener.stop()


def on_release(key):
    try:
        current.remove(key)
    except KeyError:
        pass

with Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()
``
Matt W
  • 129
  • 1
  • 7
  • You may be running into conflicts trying to use `pynput` and `pyautogui` at the same time since they both process user-events. You can verify this by writing something that only uses the former, and see if there are similar issues. – martineau Oct 02 '19 at 20:25
  • Thanks. Would you consider this issue to be unexpected? It seems to make sense to me that it would occur, regardless of which module I use to type the string. I tried using pynput's keyboard.type method but it simply wouldn't trigger anything for some reason. – Matt W Oct 02 '19 at 20:32
  • I successfully got pynput's keyboard.type(insert.timestamp) to work but, as I suggested, the issue is the same. I'm happy it types, though, because pyautogui would not work for OSX users. Thanks! – Matt W Oct 02 '19 at 20:40
  • No it wouldn't be much of a surprise, as it's somewhat inherent or intrinsic to what each module does. There might be workarounds…or perhaps you can accomplish everything you need just using `pyautogui`. – martineau Oct 02 '19 at 20:40
  • Working now with entirely pynput but problem persists. – Matt W Oct 02 '19 at 20:41
  • Then you need to modify your question or ask a new, simpler one. The goal should be to provide a [mre] of the issue. – martineau Oct 02 '19 at 20:42
  • Sorry. I'm new to all of this. I will rework the example code. – Matt W Oct 02 '19 at 20:48

1 Answers1

4

You can use pyautogui.keyUp("shift") before you type.

def insert():
f=open('tc.txt')
line=f.readlines()
insert.timestamp=(line[0])
time.sleep(.1)
pyautogui.keyUp("shift")
pyautogui.typewrite(insert.timestamp)
ItayMiz
  • 669
  • 1
  • 7
  • 14
  • 1
    Good idea! This works well. That said, now that I have the text being written with pynput (preferred for multi-platform reasons), is there a similar pynput functionality or should I go back to my pyautogui solution and use osascript on Macs? – Matt W Oct 02 '19 at 20:55
  • 1
    Use `keyboard.release(Key.shift)` (Where keyboard = Controller() ) – ItayMiz Oct 02 '19 at 21:01
  • 1
    WORks PERfectly!... just kidding. Great solution, thanks so much! How do I mark the question as solved? – Matt W Oct 02 '19 at 21:07