Skip to content

Instantly share code, notes, and snippets.

@phil-flip
Last active April 25, 2021 18:40
Show Gist options
  • Save phil-flip/f1e3781a82f989df2718112aebfcc6b3 to your computer and use it in GitHub Desktop.
Save phil-flip/f1e3781a82f989df2718112aebfcc6b3 to your computer and use it in GitHub Desktop.
MouseWheelie - This script is designed to be able to use the mouse wheel in Space Engineers without any mods. (or any other Game that relies on Keypresses from 0-9)
# MouseWheelie by Phil
# This script is designed to be able to use the mouse wheel in Space Engineers without any mods on Windows. (of any other Game that relies on Key presses from 0-9)
# This is my first time using Python so sorry if it looks messy. It was designed to be functional.
# Dependencies needed: pynput, pygetwindow
# DISCLAIMER:
# This solution isn't perfect and it's recommended to rest around in a creative world first, before using in any production.
# Known bugs that cannot be solved with this script/my abilities to python:
# - Scrolling in your ship WILL trigger the actions mapped to the hotbar (thi s only is a problem, when you are using the the script without whitelisting keys)
# - Blocks that have multiple options will select a different block, when scrolled (as the game is slightly faster, then the script can switch)
from pynput.keyboard import Controller, Listener as ListenerKeyboard, Key
from pynput.mouse import Listener as ListenerMouse, Button
import pygetwindow as gw
import time
# config
useWhitelist = True # if set to True, the configured buttons need to be pressed in order to use the wheel. This is safer to use with ships, as a 2nd input is needed to scroll. (blacklistKey and blacklistMouseKey turn to whitelists)
blacklistKey = [ ] # either set it to a key with 'a', set it to None to disable it or lookup up a special key: https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key
blacklistMouseKey = [ Button.x1 ] # there is no list for this in the documentation. pelase use debug to get the name or set it to None to disable it
gameTitle = 'Space Engineers' # Write here the game Title, use the debugging tool to get it
invertScrolling = -1 # allowed values are -1 and 1 (those are the values outputed by scrolling up or down)
debug = False # use this to check, if the values are set correctly, if set to False, no Output will show
# dont change them, wouldn't effect
invPos = 0
keyPressed = useWhitelist
def on_scroll(x, y, dx, dy):
if debug:
print('Scrolling: ', dy)
print('Game title: ', gw.getActiveWindow().title)
# check if game title is correct
if not keyPressed and gw.getActiveWindow().title == gameTitle:
global invPos
if dy == invertScrolling: invPos += 1
else: invPos -= 1
# check if end of inv and revert
if invPos == 10: invPos = 0
if invPos == -1: invPos = 9
if debug: print('New invPos: ', invPos)
# presses key
Controller().press(str(invPos))
# is needed, otherwise too fast for the game to handle
time.sleep(0.015)
Controller().release(str(invPos))
# sets the value
def setKeyValue(pressed):
global keyPressed
# change keyPressed to comply with whitelist
if useWhitelist: keyPressed = not pressed
else: keyPressed = pressed
if debug: print('Key found! New pressed value: ', keyPressed)
# checks if keyboard value is the same as in config
def checkKeyValue(key, pressed):
if debug: print('New key event: ', key)
for oneKey in blacklistKey:
# normal keys
if hasattr(key, 'char') and key.char == oneKey: setKeyValue(pressed)
# special key support
if not hasattr(key, 'char') and key == oneKey: setKeyValue(pressed)
# checks if mouse key value is the same as in config
def on_click(x, y, button, pressed):
if debug: print('New mouse key event: ', button)
for oneKey in blacklistMouseKey:
if button == oneKey: setKeyValue(pressed)
def on_press(key):
checkKeyValue(key, True)
def on_release(key):
checkKeyValue(key, False)
# check for keys and mouse wheel actions
with ListenerKeyboard(on_press=on_press, on_release=on_release) as listenerKey, ListenerMouse(on_scroll=on_scroll, on_click=on_click) as listenerMouse:
listenerKey.join()
listenerMouse.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment