Mouse joystick script

Official forum for open source FreePIE discussion and development.
Post Reply
V8LENTIN
One Eyed Hopeful
Posts: 1
Joined: Fri Feb 13, 2015 11:15 am

Mouse joystick script

Post by V8LENTIN »

Hi,

I sometimes play IL2 Sturmovik (flight simulator with WWII planes) without a joystick and it doesn't have mouse as joystick support. I used PPJoy and GlovePIE for that but recently installed Win 7 64 and I don't want to use that signing test mode for PPJoy.

So I switched to FreePIE and vJoy, but the problem with some scripts I found online is that the mouse.deltax keeps adding even if the cursor is on the limit of the screen. So of course, when you come back to the middle of the screen, the cursor is centered but the joystick is not anymore... not to mention you have to start FreePIE with the cursor centered and so on... so no go.

I thought I could set some limits and so on but the mouse.deltax keeps drifting no matter what.
Then I thought FreePIE uses Python, did some search online, and found this script where we can read screen size and a cursor position that runs from 0 on the left of the screen, to maximum resolution to the right (I'm talking about X axis now). This is it: http://nullege.com/codes/show/src@m@o@m ... tCursorPos

So I just wrote the code below and now I have the cursor in game in the middle of the screen telling me that the vJoy axis are also centered. The only downside is that you have to calibrate (I initially thought about adding some values that each user ca change to max. controls on screen boundaries) but there's not much to it, just focus the next button in "Calibrate" window for USB joystiks, move the mouse to the top and bottom and left to right when setting X/Y axis and hit ENTER to save setting.

Hope it helps :)

Code: Select all

from System import Int16
from ctypes import windll, Structure, c_ulong, byref

class POINT(Structure):
	_fields_ = [("x", c_ulong), ("y", c_ulong)]

if starting:
	vJoy0_stat = 0
	vJoy[0].x = 0
	mouse_x = 0
	mouse_y = 0
	screen_x = windll.user32.GetSystemMetrics(0)
	screen_y = windll.user32.GetSystemMetrics(1)
	pt = POINT()

if keyboard.getPressed(Key.CapsLock): // enable/disable mouse vJoy X/Y control by pressing CapsLock
	if vJoy0_stat == 0: vJoy0_stat = 1
	else: vJoy0_stat = 0

if vJoy0_stat == 1:
	windll.user32.GetCursorPos(byref(pt))
	mouse_x = pt.x
	mouse_y = pt.y
	vJoy[0].x = mouse_x - (screen_x / 2)
	vJoy[0].y = mouse_y - (screen_y / 2)

#diagnostics.watch(screen_x)
#diagnostics.watch(screen_y)
#diagnostics.watch(mouse_x)
#diagnostics.watch(mouse_y)
diagnostics.watch(vJoy[0].x)
diagnostics.watch(vJoy[0].y)
ariefansori
One Eyed Hopeful
Posts: 4
Joined: Wed Dec 17, 2014 4:06 pm

Re: Mouse joystick script

Post by ariefansori »

nice idea :idea:
as for me, I'm using XMouseButtonControl to lock my cursor :woot
snp
One Eyed Hopeful
Posts: 1
Joined: Sun May 10, 2015 2:39 am

Re: Mouse joystick script

Post by snp »

https://github.com/snp306/Joyness
Hi, i made my solution here. Anyway i think you should be credited as it's the same mouse pointer idea. Thank you :).
Awasaky
One Eyed Hopeful
Posts: 3
Joined: Fri May 15, 2015 9:29 am

Re: Mouse joystick script

Post by Awasaky »

Hi all.
I tryed modify this script to made it less system-specific:

Code: Select all

from System import Int16
from ctypes import windll, Structure, c_ulong, byref

class POINT(Structure):
   _fields_ = [("x", c_ulong), ("y", c_ulong)]

#  vJoy0_stat is boolean, so declared as Boolean.
#  multipler_y uses for made screen space same scale, than mouse moves. It only one, cos most screens is have shorter only 1 side - height.
# for example if you have 1920x1080 screen then you need use square part of screen for joystick moves.

if starting:
   vJoy0_stat = False
   vJoy[0].x = 0
   mouse_x = 0
   mouse_y = 0
   screen_x = windll.user32.GetSystemMetrics(0)
   screen_y = windll.user32.GetSystemMetrics(1)
   pt = POINT()
   multipler_y = 32768 / screen_y

# rewrited part to Boolean variable

if keyboard.getPressed(Key.CapsLock): # enable/disable mouse vJoy X/Y control by pressing CapsLock
   if vJoy0_stat:
		vJoy0_stat = False
		vJoy[0].x = 0
   		vJoy[0].y = 0
   else: vJoy0_stat = True

# last 2 rows down added cos if you use 2 screens, script made error, if you move from second screen to first.

if vJoy0_stat:
   windll.user32.GetCursorPos(byref(pt))
   if pt.x > 65536: mouse_x = 0
   else: mouse_x = pt.x
   if pt.y > 65536: mouse_y = 0
   else: mouse_y = pt.y
 
# multipler usage to transfer mouse pointer on screen position to joystick tilt
   vJoy[0].x = multipler_y * (mouse_x - (screen_x / 2))
   vJoy[0].y = multipler_y * (mouse_y - (screen_y / 2))

Here is video example of this script
http://www.youtube.com/watch?v=fbvUcz0CFWM
Blamm-0
One Eyed Hopeful
Posts: 6
Joined: Sat May 30, 2015 4:28 pm

Re: Mouse joystick script

Post by Blamm-0 »

I am also known as Vicious Lee, and in 2009 I put together this guide for mouse-as-joystick for IL2:

http://www.mission4today.com/index.php? ... ++joystick

I haven't played since then, and I'd like to get it going again, but now I'm on Win 7 64 bit and I dread the prospect of going through that whole setup again -- it took forever, quite frankly.

V8LENTIN and you others, can you explain in more detail how you set up mouse as joystick in IL-2 with freepie or glovepie, and vjoy? I'm familiar with vjoy but not the other things.

blamm0
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Re: Mouse joystick script

Post by samworthington »

This looks like it could be really good for what I need. - I want to translate the mouse movement so it appears as joystick movement in different applications.

I'd appreciate some help in getting it working. I have installed vJoy_205_050515 and FreePie. However, when I paste your script into a new script within FreePie and run the script, I get the error "Unexpected Indent". Do you have any suggestions?

Sam
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Re: Mouse joystick script

Post by samworthington »

Thanks for this, I'm just struggling a bit getting it to work.

First I got an error saying "unexpected toke '//' "

I deleted "// enable/disable mouse vJoy X/Y control by pressing CapsLock" and nothing happens. When I look at the Windows joystick calibration of the vJoy device and calibrate with displayed values, I notice the X-axis and Y-axis values are very high (15423 and 16294) respectively. I also got an error on one of the runs on line 24 "Arithmetic operation resulted in an overflow"

I'd be really grateful for help in getting it to work.
CyberVillain
Petrif-Eyed
Posts: 2166
Joined: Mon Jun 22, 2009 8:36 am
Location: Stockholm, Sweden

Re: Mouse joystick script

Post by CyberVillain »

// is not a valid comment in python is should be #
samworthington
One Eyed Hopeful
Posts: 12
Joined: Tue Jun 16, 2015 4:49 am

Re: Mouse joystick script

Post by samworthington »

Thanks you.

I'm now got the script discussed here http://www.mtbs3d.com/phpBB/viewtopic.p ... 50#p158950 working, and am trying to modify so a button press is needed to make the joystick functionality working.

Sam
ajestkingscrown
One Eyed Hopeful
Posts: 1
Joined: Sat Jun 13, 2020 10:18 pm

Re: Mouse joystick script

Post by ajestkingscrown »

can u guide me as to how i can also add yaw controls to this flight script?i would like it to be the same as how caps can alternate between head movement and aircraft movement....
any help appreciated....
Post Reply

Return to “FreePIE”