Skip to content

Instantly share code, notes, and snippets.

@IslandJohn
Last active November 15, 2020 14:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IslandJohn/30fb66b8a3d0161ff6fe15053c9f3d01 to your computer and use it in GitHub Desktop.
Save IslandJohn/30fb66b8a3d0161ff6fe15053c9f3d01 to your computer and use it in GitHub Desktop.
Joystick Gremlin plugin to remap the short and long press of a physical push button to two virtual buttons.
import gremlin
import threading
import time
from gremlin.user_plugin import *
mode = ModeVariable(
"Mode",
"The mode to use for this instance"
)
long_p = IntegerVariable(
"Long Delay (ms)",
"Long Delay (ms)",
400,
100,
10000
)
joy_1 = PhysicalInputVariable(
"Physical Button",
"Physical Button",
[gremlin.common.InputType.JoystickButton]
)
vjoy_1 = VirtualInputVariable(
"Virtual Short",
"Virtual Short",
[gremlin.common.InputType.JoystickButton]
)
vjoy_2 = VirtualInputVariable(
"Virtual Long",
"Virtual Long",
[gremlin.common.InputType.JoystickButton]
)
decorator_1 = joy_1.create_decorator(mode.value)
start_t = 0
short_t = None
long_t = None
def change_state(pressed, vjoy):
global long_p, vjoy_1, vjoy_2, start_t, short_t, long_t
v1 = vjoy[vjoy_1.value["device_id"]].button(vjoy_1.value["input_id"])
v2 = vjoy[vjoy_2.value["device_id"]].button(vjoy_2.value["input_id"])
if pressed:
if short_t is not None: short_t.cancel()
if long_t is not None: long_t.cancel()
v1.is_pressed = v2.is_pressed = False
start_t = time.monotonic()
long_t = threading.Timer(long_p.value/1000.0, long_press, [v2])
long_t.start()
else:
if long_t is None: # reset or bad state
v1.is_pressed = v2.is_pressed = False
else:
long_t.cancel()
if v2.is_pressed: # long press
v2.is_pressed = False
long_t = None
else: # short press, released before long press delay
v1.is_pressed = True
v2.is_pressed = False
if short_t is not None: short_t.cancel()
short_t = threading.Timer(time.monotonic()-start_t, short_release, [v1])
short_t.start()
def long_press(v2):
v2.is_pressed = True
def short_release(v1):
v1.is_pressed = False
@decorator_1.button(joy_1.input_id)
def joy_event(event, vjoy):
change_state(event.is_pressed, vjoy)
if joy_1.value is not None:
vjoy_init = gremlin.input_devices.VJoyPlugin.vjoy
change_state(False, vjoy_init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment