Skip to content

Instantly share code, notes, and snippets.

@jake-lewis
Last active January 21, 2024 13:36
Show Gist options
  • Save jake-lewis/e89207690cbc777fa82e9ae248b683ab to your computer and use it in GitHub Desktop.
Save jake-lewis/e89207690cbc777fa82e9ae248b683ab to your computer and use it in GitHub Desktop.
freePIE mouse as joystick

Installation

  1. Install vJoy
  2. Install freePIE
  3. In freePIE: new file -> copy script below -> save as'BLAH'
  4. Run script (F5)
  5. Launch DCS (on my machine has to be after a joystick is added, but you'll probably be fine running dcs first)
  6. Settings -> Controls -> Select Su-25T (or any module) -> Select 'Axis Commands' in category dropdown
  7. Bind pitch and Roll (can double click on field in vJoy Device column) to JOY_X, JOY_Y respectively
  8. Click OK (I cannot state how many times I quit accidentally and lost ~15mins worth of keybinds)
  9. Play russian national anthem
  10. ???
  11. Profit

Usage

  1. Run FreePIE
  2. Load script (from file or raw text, but might as well save it somewhere)
  3. F5 to run

If you get an error stating joy[0] doesn't exist, make sure vJoy is running, and using the Configure vJoy tool, have the virtual joystick device stored in the number 1 slot. After a restart mine went to slot 2, and the script stopped working

  1. Mouse x & y will change vJoy[0].x & vJoy[0].y (vJoy can have multiple joysticks configured, e.g. vJoy[0], vJoy[1])

Emulates a joystick by returning to center, not ideal since you have to keep scrolling the mouse for constant pitch up, not sure how to handle this)

  1. Hold CAPSLOCK to toggle to "freelook" (mouse now changes vJoy[0].rx & vJoy[0].ry)

This doesn't automatically centre, use middle mouse button to recenter

Alt code for toggle freelook is included, switch commented sections lines 41-47 to use

Keybinds will probably look something like this:

image

Script

# init
if starting:
	centreX = 0.015
	centreY = 0.03
	sensX = 50
	sensY = 50
	viewSensX = 10
	viewSensY = 10
	system.setThreadTiming(TimingTypes.HighresSystemTimer)
	system.threadExecutionInterval = 20
	x = 0
	y = 0
	rx = 0
	ry = 0
	freelook = False
	viewLatch = False
	moveLatch = False

# mouse axis
if mouse.deltaX:
	if freelook:
		rx += mouse.deltaX * viewSensX
	else:
		moveLatch = True
		x += mouse.deltaX * sensX
		if abs(x) > vJoy[0].axisMax:
			x = vJoy[0].axisMax * x / abs(x)
else:
	x /= (1.0 + centreX)

if mouse.deltaY:
	if freelook:
		ry += mouse.deltaY * viewSensY
	else:
		moveLatch = True
		y += mouse.deltaY * sensY
		if abs(y) > vJoy[0].axisMax:
			y = vJoy[0].axisMax * y / abs(y)
else:
	y /= (1.0 + centreY)

# recenter
if (not freelook and viewLatch):
	rx = 0
	ry = 0
	viewLatch = False

#keyboard override mouse
if (moveLatch and keyboard.getPressed(Key.Space) or keyboard.getPressed(Key.Z)):
	x = 0
	y = 0
	moveLatch = False

#code for hold freelock
freelook = mouse.getButton(3) or mouse.middleButton
if (mouse.getPressed(3)):
	viewLatch = True
	
#code for toggle freelook
#toggle = keyboard.getPressed(Key.CapsLock)
#if (toggle):
#	freelook = not freelook


# vJoy axis mapping
vJoy[0].rx = rx
vJoy[0].ry = ry
vJoy[0].x = x
vJoy[0].y = y

diagnostics.watch(vJoy[0].x)
diagnostics.watch(vJoy[0].y)
diagnostics.watch(vJoy[0].rx)
diagnostics.watch(vJoy[0].ry)
diagnostics.watch(freelook)
diagnostics.watch(moveLatch)
diagnostics.watch(mouse.getButton(3))
@pertetual1
Copy link

thx for helping mate

@GrisMouche
Copy link

GrisMouche commented Jun 17, 2023

Could you add a feature to switch the control mode between absolutely and relatively? Means in relatively mode we can always get a auto re-center function, until we switch it off. Or just make it auto re-center while we press and hold a key.

@ChaosFoxOverlord
Copy link

ChaosFoxOverlord commented Jan 14, 2024

Thank you very much; helped a lot after only a little bit of modifiycation - for running old games w/o Joystick. Made a guide and linked your Gist: https://steamcommunity.com/sharedfiles/filedetails/?id=3139443027

EDIT: ...and forked it, giving credit to you and putting it under a GPL-v3 Licence. I hope, this works for you: https://gist.github.com/ChaosFoxOverlord/5b8d59b3591f02f4f3b2701e51fa6547

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment