Search code examples
pythonpynput

What's causing this error "struct.error: required argument is not an integer"?


i'm trying to make a script where upon opening it, it asks whether i want to start the scroller function which scrolls or the presser function that presses the right key and waits for 5 seconds
But when the presser function is triggered it gives me this error

struct.error: required argument is not an integer
What's causing this error?

import time
from pynput.keyboard import Key,Controller
from pynput.mouse import Button, Controller
time.sleep(1)
n = 0
keyboard = Controller()
mouse = Controller()
def presser():
    global n
    n = n+1
    keyboard = Controller()
    keyboard.press(Key.right)
    keyboard.release(Key.right)
    if n == 1:
        print('Key Pressed 1 time!')
    else:
        print('Key Pressed', n ,'times!')    
    time.sleep(5)
    presser()
def scroller():
    mouse.scroll(0,-10)
    scroller()
    time.sleep(1)
def starter():
    x = input(' clicker or scroller ').lower()
    if x == 'clicker':
        presser()
    elif x == 'scroller':
        scroller()
starter()

Error with traceback:-

  File "Funk.py", line 30, in <module>
    starter()
  File "Funk.py", line 27, in starter
    presser()
  File "Funk.py", line 12, in presser
    keyboard.press(Key.right)
  File "/home/asg/anaconda3/lib/python3.7/site-packages/pynput/mouse/_base.py", line 90, in press
    self._press(button)
  File "/home/asg/anaconda3/lib/python3.7/site-packages/pynput/mouse/_xorg.py", line 95, in _press
    Xlib.ext.xtest.fake_input(dm, Xlib.X.ButtonPress, button.value)
  File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/ext/xtest.py", line 100, in fake_input
    y = y)
  File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/protocol/rq.py", line 1459, in __init__
    self._binary = self._request.to_binary(*args, **keys)
  File "/home/asg/anaconda3/lib/python3.7/site-packages/Xlib/protocol/rq.py", line 1141, in to_binary
    return self.to_binary(*varargs, **keys)
  File "<string>", line 2, in to_binary
struct.error: required argument is not an integer

Solution

  • You have two import statements that import something called Controller from two different modules. Presumably these are different classes with different uses. I think the second import is replacing the first, so every time you are referring to Controller it is the one from the mouse module. It appears in presser() you want to be using the one from the keyboard module.

    So you are calling the mouse controller's press() function with an argument intended for the keyboard controller's function. I suspect this is the cause of the error.

    You need to write your imports to make these two classes distinct. You could do this by simply importing the entire module and fully-qualifying the class names. Or you could do it by aliasing the classes on input, such as:

    from pynput.keyboard import Key, Controller as KeyboardController
    from pynput.mouse import Button, Controller as MouseController