Search code examples
pythonpynput

Is is possible to send a variable using python's pynput?


I have made a python script to do some math for me. I then used this pynput script

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

keyboard = Controller()

def type(char):
    keyboard.press(char)
    keyboard.release(char)

output = "144"
type(output)

The aim is to send the variable "Output" from my keys and that var - output - is always an integer with a length between 1 digit and 3 digits. When I run it it replies with

  File "C:/Users/matth/OneDrive/Documents/My programs/TTRockstars/BIG BREAK/type.py", line 14, in <module>
    type(output)
  File "C:/Users/matth/OneDrive/Documents/My programs/TTRockstars/BIG BREAK/type.py", line 8, in type
    keyboard.press(char)
  File "C:\Users\matth\AppData\Local\Programs\Python\Python37\lib\site-packages\pynput\keyboard\_base.py", line 362, in press
    resolved = self._resolve(key)
  File "C:\Users\matth\AppData\Local\Programs\Python\Python37\lib\site-packages\pynput\keyboard\_base.py", line 556, in _resolve
    raise ValueError(key)
ValueError: 144

I don't know if pynput is the right way to go?? or is there an alternative?


Solution

  • A suggestion:python has a function called type().In your code,you have redefined it.And your code can be:

    import pynput
    from pynput.keyboard import Key, Controller
    import time
    
    keyboard = Controller()
    
    def Type(char):
        pressList = list(char)
        for i in pressList:
            keyboard.press(i)
            keyboard.release(i)
    
    output = "144"
    Type(output)
    

    When you run this code.It will press "1","4","4".