my code stock.com

Arief Ansori

freePIE - mouse to vJoy
by Arief Ansori

emulate vJoy using mouse
download :
andersmalmgren.github.io/FreePIE
vjoystick.sourceforge.net

Snippet options

Download: Download snippet as freepie-mouse-to-vjoy.py.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

from System import Int16

def update():
   global yaw
   global pitch
   global roll
   global lastYaw
   global x
   global y
   global z

if starting:
	# mouse
	enabled = False
	mousex = 0
	mousey = 0
	a = 0
	b = 0
	c = 0
	d = 0
	throttle = 200
	brake = 1000
	mod = 0
	
	# trackIR
	yaw = 0
	pitch = 0
	roll = 0
	x = 0
	y = 0
	z = 0
	
global mouse_sensitivity, steering_center
mouse_sensitivity = 4
steering_center = 1


# mouse sensitivity modifier
mod = int(mousex * 0.002)
if mod < 0:
	mod = -mod
mouse_sensitivity += mod


# mouse steering control
if (enabled):
	mousex += mouse.deltaX * mouse_sensitivity
else:
	mousex = 0
	mousey = 0
	yaw = 0
	
trackIR.yaw		=	math.degrees(yaw) * 0.002
trackIR.pitch	=	0
trackIR.roll	=	0
trackIR.x		=	x
trackIR.y		=	y
trackIR.z		=	z

if (mouse.middleButton):
	mousex = 0
	yaw = 0
	
	
# throttle and brake
if mouse.leftButton and mouse.rightButton and (enabled):
	mousey = mousey
elif mouse.leftButton and (mousey <= 0) and (enabled): 
	mousey -= throttle
elif mouse.rightButton and (mousey >= 0) and (enabled): 
	mousey += brake
else:
	mousey = 0
	
	
#################################################################
#   
# auto center steering when mouse x stop moving
#
# a += mouse.deltaX
# b += mouse.deltaX
#
# if filters.stopWatch(True,600):
#   c = a + 0
#   
# if filters.stopWatch(True,30):
#   d = b + 0
#
# if (c - d == 0):
#	if mousex < -500:
#		mousex += (steering_center + (mod**2))
#	elif mousex > 500:
#		mousex -= (steering_center + (mod**2))
#	 
##################################################################


# limit range movement
if (mousex > Int16.MaxValue): 
	mousex = Int16.MaxValue
	yaw = yaw
elif (mousex < -Int16.MaxValue):
	mousex = -Int16.MaxValue
	yaw = yaw
elif (mousex <= 0) and (yaw > 0):
	yaw -= 4
elif (mousex >= 0) and (yaw < 0):
	yaw += 4
else:
	yaw += mouse.deltaX
	
if (mousey > Int16.MaxValue):
	mousey = Int16.MaxValue
elif (mousey < -Int16.MaxValue):
	mousey = -Int16.MaxValue


	


# mouse to vJoy
vJoy[0].x = mousex
vJoy[0].y = mousey


# assign button
vJoy[0].setButton(0,int(keyboard.getKeyDown(Key.LeftShift)))
vJoy[0].setButton(1,int(keyboard.getKeyDown(Key.LeftControl)))
vJoy[0].setButton(2,int(keyboard.getKeyDown(Key.Q)))
vJoy[0].setButton(3,int(keyboard.getKeyDown(Key.W)))
vJoy[0].setButton(4,int(keyboard.getKeyDown(Key.E)))
vJoy[0].setButton(5,int(keyboard.getKeyDown(Key.A)))
vJoy[0].setButton(6,int(keyboard.getKeyDown(Key.S)))
vJoy[0].setButton(7,int(keyboard.getKeyDown(Key.D)))


# watch
diagnostics.watch(enabled)
diagnostics.watch(vJoy[0].x)
#diagnostics.watch(vJoy[0].y)
#diagnostics.watch(mouse_sensitivity)
#diagnostics.watch(mod)
#diagnostics.watch(yaw)
diagnostics.watch(trackIR.yaw)


# toggle mouse steering
toggle = keyboard.getPressed(Key.NumberPad0)

if toggle:
    enabled = not enabled

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.