from datetime import datetime, timedelta import gremlin from gremlin.user_plugin import * axis = PhysicalInputVariable( "Axis", "The axis that will trigger scrolling.", [gremlin.common.InputType.JoystickAxis], ) mode = ModeVariable( "Mode", "The mode in which the axis will be mapped.", ) response_coefficient = FloatVariable( "Scroll Speed", "Adjusts the rate at which the target button is pressed, relative to the axis strength.", 1.0, ) deadzone = FloatVariable( "Deadzone", "Applies a deadzone to both sides of the axis.", 0.0, ) # NB: Not a real boolean; invert.value returns 0 or 2 invert = BoolVariable( "Invert", "Reverse the direction that the axis scrolls.", False, ) axis_decorator = axis.create_decorator(mode.value) # Stateful data last_timestamp = datetime.now() @axis_decorator.axis(axis.input_id) # @gremlin.input_devices.periodic(1) def handle_axis(event): global last_timestamp # axis_value = joy[axis.device_guid].axis(axis.input_id).value axis_value = event.value if (abs(axis_value) < deadzone.value): return delta = (datetime.now() - last_timestamp) / timedelta(milliseconds=1) if delta >= (1 - abs(axis_value)) * 250 * response_coefficient.value: direction = 1 if axis_value > 0 else -1 if invert.value: direction = direction * -1 gremlin.sendinput.mouse_wheel(direction) last_timestamp = datetime.now()