Mouse Movement to Controller stick/keyboard keys

Post gaming related scripts
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Mouse Movement to Controller stick/keyboard keys

24 Jul 2016, 20:31

I would like a script that will allow me to map the movement of my mouse to that of the WSAD keys on my keyboard or, the more preferable option, the movement of the left analog stick of a connected Xbox One controller. Thanks for the help
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

25 Jul 2016, 12:04

If I have time tonight I'll make something, shouldn't be a problem.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

25 Jul 2016, 16:36

Here is a simple joystick simulator, add whatever functions you want it to perform. See comments in code, it's pretty straight forward.

Source code:

Code: Select all

;	;	;	;	;	;	;	;	;	;	;	;	;	;	;	;
;	Author: helgef
;	Date: 2016-07-25
;	Description:
;	Simple joystick simulator.
;	The circle displayed on screen is a visual aid for the control.
;	There are two circles, when the mouse is inside the inner circle
;	the control does nothing, this corresponds to having the joystick
;	in its default position
;	The outer ring is divided into twelve (invisible) segments.
;	Moving the mouse over each segment, triggers a different action, these are
;	tooltips which helps you identify which code in the function: action() to 
;	modify to appropriate key-mappings/actions.
;	
;	Notes: 	
;			-The origin of the circle is defined relative of the gui. 
;			-CoordMode, Mouse, Window is used
;			-#s (Winkey+s) toggles on/off.
;			-Escape exit at any time.
;

#NoEnv  																; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  														; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  											; Ensures a consistent starting directory.

; Display on screen visual aid for joystick control
Gui, Controller: New
Gui, Controller: +ToolWindow -Caption HWNDstick
Gui, Controller: Color, FFFFFF
Gui, Controller: Add, Picture, X0 Y0 , circle.png
Gui, Controller: Show,,Controller
WinSet,TransColor,FFFFFF,ahk_id %stick%
SetMouseDelay,-1

; Constants and such.
r:=100		; Circle radius
dr:=0		; Bounce back when hit outer circle edge, in pixels.
ir:=80		; Radius of the inner circle, inside no action.
pi:=3.1415	; Approx pi.
OX:=100		; Circle center.
OY:=100
freq:=10	; update frequency, in ms.
toggle:=1	; On/off parameter for the hotkey.

segmentEndAngles:=Object()						; Each segment is defined by its angle, segment 1,...,12 -> angle pi/6,pi/3,...,2*pi. (Unfortuantley its clockwise, with 0 being at three o'clock)
Loop,12
	segmentEndAngles[A_Index]:=pi/6*A_Index

; End autoexec.
	
; Hotkeys
#s::
	if toggle
	{
		WinActivate, ahk_id %stick%
		WinWaitActive, ahk_id %stick%
		SetTimer,mouse2joystick,%freq%
		MouseMove,OX,OY
	}
	else
	{
		SetTimer,mouse2joystick,Off
		ToolTip
	}
	toggle:=!toggle
return

ESC::ExitApp

; Labels

mouse2joystick:	
	mouse2joystick(r,ir,dr,OX,OY)
return

; Functions

mouse2joystick(r,ir,dr,OX,OY)
{
	; r is the radius of the outer circle
	; if is the radius of the inner circle
	; dr is a bounce back parameter.
	; OX is the x coord of circle center
	; OY is the y coord of circle center
	CoordMode,Mouse,Window
	MouseGetPos,X,Y
	X-=OX										; Move to cirle coord system
	Y-=OY
	RR:=sqrt(X**2+Y**2)
	if (RR>r)									; Check if outside circle
		MouseMove,X*(r-dr)/RR+OX,Y*(r-dr)/RR+OY ; Calculate point on circle, move back to screen/window coords, and move mouse.
	if (RR>ir)									; Check if outside inner circle
	{
		; Calculate segement
		phi:=getAngle(X,Y)
		seg:=getSegment(phi)
		; Call action function.
		action(seg)
	}
	else
	{
		ToolTip,*	; Inside the inner circle, control is in default position
	}
	
}

action(seg)
{	
	; Define what to do, move the mouse around the circle to
	if (seg=1 || seg=12)
		ToolTip, Right      
	else if (seg=2)
		ToolTip, Down  Right 
	else if (seg=3 || seg= 4)
		ToolTip, Down       
	else if (seg=5)
		ToolTip, Left  Down 
	else if (seg=6 || seg=7)
		ToolTip, Left       
	else if (seg=8)
		ToolTip, Left      Up 
	else if (seg=9 || seg=10)
		ToolTip, Up        
	else if (seg=11)
		ToolTip, UP    Right 
	else
		return -1 ; error
	return
}

getSegment(phi)
{
	global segmentEndAngles
	Loop 12
		if(phi<segmentEndAngles[A_Index])
			return A_Index
	return -1 ; error
}
getAngle(x,y)
{
	global pi
	x:=(x=0)?1:x 		; Avoid dividing with zero. No error is thrown it seems.
	phi:=atan(y/x)
	if (x<0 && y>0)
		return phi+pi
	if (x<0 && y<0)
		return phi+pi
	if (x>0 && y<0)
		return phi+2*pi
	return phi
}

; End
Put image in same folder as script, run.
mouse2joystick.ahk
(3.88 KiB) Downloaded 535 times
circle.png
circle.png (2.91 KiB) Viewed 10006 times
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Re: Mouse Movement to Controller stick/keyboard keys

25 Jul 2016, 18:34

I'm having a bit of trouble getting it all to work. Sorry, this is my first time using AutoHotKey and I'm not sure if I am doing something wrong or what. I've tried every key I can imagine to enable the toggle and I can't get it to work. I've got the picture appearing on screen, but my mouse doesn't lock to it. Thanks for your help as well Helgef.

edit# I found the correct key bind literally right after I typed that message and I am getting ready to test this script out. I really appreciate your help with this Helgef. Thanks so much.
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Re: Mouse Movement to Controller stick/keyboard keys

25 Jul 2016, 19:56

I tested the script in a Custom match in Call of Duty Black ops 2 and I could not get the effect I needed with this script. I want to be able to move the mouse along the x and y axis and have those correspond to those of which the player would be making in game. Thanks again
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

26 Jul 2016, 01:56

First, did you modify the action() function? It just shows tooltips, you need to add your key presses there. Eg, under the first block, segment 12 and 1, you add press down your right movement key, also release previously pressed keys.
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Re: Mouse Movement to Controller stick/keyboard keys

26 Jul 2016, 13:57

I hadn't at the time, but I have now and I got the WSAD keys mapped to the corresponding segments. I opened NotePad to test the keystrokes and I noticed there was often the D key pressed when the mouse went in between segments 8 and 9 I believe. I have D set to segments 1 and 12, 2, and 11. When I tested the script in Call of Duty, I couldn't move and there was a slight mouse drift off to the left even with the Sensitivity set to 0.01, it seemed to move faster than that. I'm not sure if that's something with the game, the script, or the fact that when I toggle the script on, I have to click on the game screen behind the Circle picture. I also have a small request to make and I'm not sure how much trouble it would be, I would like for the mouse cursor to be reset back within the center of the circle as soon as the movement stops. For the use of the script, this will be a crucial part. Thanks again for all your help and I apologize if I'm asking for a lot.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

26 Jul 2016, 16:23

If you post your code I can look at it.
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Re: Mouse Movement to Controller stick/keyboard keys

26 Jul 2016, 17:08

The code isn't changed much from what you gave me, all I really did was, since I couldn't figure out how to keep the ToolTip with the button press, change your "Tooltip, Right" code to "Send,D" but of course for the following directions and buttons

Code: Select all

[attachment=0]mouse2joystick (1).ahk[/attachment]
Attachments
mouse2joystick (1).ahk
(3.8 KiB) Downloaded 265 times
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

26 Jul 2016, 19:28

I made some modifications. I tested it in quake 3 and it works fine, but quake does some funny things to the mouse, so I had too make some special adjustments for that, that's not in the script here though, the point is your game might require some special tweaking of the script, it will be hard for me to help since I don't have it.
I have added a hotkey #d - that is winkey+d - to enable/disable movment of the circle.
Also, moving back to the inner circle will send all (wasd) keys up, and mouse will fall back to the center. I adjusted your modifications to the action() function.
You should do this: Edit the variable gameExe in this script, read the comment, save, (if you want to test in notepad first, change gameExe:="notepad.exe") open your game, when its ready to play, run the script, press winkey+d, move the circle to where you want it, press winkey+d again, press winkey+s and hope it works.
If you have problems, the more info you can give, the easier it is to help.
Another thing to try if you have problems, put the circle over the center (approximately is fine) of the game screen, see what happens.

Code:
Spoiler
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Re: Mouse Movement to Controller stick/keyboard keys

26 Jul 2016, 20:28

I just opened the script and tested it out in Black Ops 2. I managed to get it to work "properly-ish". I still had the mouse drifting off to a key when I initially loaded the script, But at some point, and I'm not sure how, I got the drifting to stop altogether. I noticed that when i disabled the script, it would still be drifting off as if the key were still being pressed down. However, this time the drifting was slightly weird as my games camera was drifting to the right, but the mouse was "pressing" my A key and moving me to the left. Also, when I tried to move the circle around, the shortkey worked and I could move it properly, though the mouse seemed to be set to return to the center point of the initial location, ie the center of the screen.


This part is only for to the script and can be disregarded for now:I would like to enlarge the segments to reach to almost the center of the circle, that isn't especially important to the use of this script.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

27 Jul 2016, 08:25

Do you have the circle outside the game window? If so, if you move it from that side, to the other side, does that change the behaviour? The game is probably moving the mouse, or trying, and the script tries to move it back. A solution could then be to have the circle centered where the game wants it, probably center of game screen. Then the circle has to be made invisible or it will obstruct game play, and cursor should be hidden to.

Edit: You can change the radius of the inner circle via the ir parameter in the script, make it smaller, say ir:=50, then the segments are reached halfway out of the circle, from the center that is, you can also make outer circle smaller, that is r. Just make sure
ir<r.
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Re: Mouse Movement to Controller stick/keyboard keys

27 Jul 2016, 12:27

I'm not sure that I actually will need the circle to be invisible as I plan on doing all of this in VR, and I dont believe the circle will appear in the headset. Also, I got the joystick to work properly by adjusting the radius of the inner circle and offsetting the circle slightly to the right of the center of the screen; however, is there a way to delay the return of the mouse to the center.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

27 Jul 2016, 12:49

Ok that's nice. I don't know what that is, VR.
About the delay, yes, if you just want to stick the mouse to the center longer, adjust the fallbackpause parameter. Or add a, eg., sleep, 200 before the movemouse in the if (actiontaken =1) block. Typed on the phone..
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Re: Mouse Movement to Controller stick/keyboard keys

27 Jul 2016, 13:16

I'm not sure what happened, but now the mouse wont reset its position at all after it is moved.

Also, VR is Virtual Reality. I've just gotten a VR Head Mounted Display, sort of. I'm streaming a VR game from my PC to my Phone and playing it in the HMD. I've set up a gun control system using a Wii Mote and nunchuck synced to my PC via bluetooth, and this script is going to be used to translate my real world movement into the game allowing my real world movements to be translated to an in-game movement. There are alternatives to something like I am doing, but those solutions tend to get a bit pricey.

Here's my current script:

Code: Select all

;	;	;	;	;	;	;	;	;	;	;	;	;	;	;	;
;	Author: helgef
;	Date: 2016-07-25
;	Description:
;	Simple joystick simulator.
;	The circle displayed on screen is a visual aid for the control.
;	There are two circles, when the mouse is inside the inner circle
;	the control does nothing, this corresponds to having the joystick
;	in its default position
;	The outer ring is divided into twelve (invisible) segments.
;	Moving the mouse over each segment, triggers a different action, these are
;	tooltips which helps you identify which code in the function: action() to 
;	modify to appropriate key-mappings/actions.
;	
;	Notes: 	
;			-The origin of the circle is defined relative of the gui. 
;			-CoordMode, Mouse, Window is used
;			-#s (Winkey+s) toggles on/off.
;			-Escape exit at any time.
;

#NoEnv  																; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  														; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  											; Ensures a consistent starting directory.

; Settings
CoordMode,Mouse,Screen
SetMouseDelay,-1

; On exit
OnExit("exitFunc")

; Constants and such.
r:=95					; Circle radius.
dr:=0					; Bounce back when hit outer circle edge, in pixels.
ir:=40					; Radius of the inner circle, inside no action.
pi:=3.1415				; Approx pi.
OX:=100+WX				; Circle center.
OY:=100+WY
freq:=10				; update frequency, in ms.
toggle:=1				; On/off parameter for the hotkey.
actionTaken:=0			; For handling quick fall back to center.
fallBackPause:=150		; Short mouse movement block after fall back.
gameExe:="notepad.exe"	; Change this to your games executable.

segmentEndAngles:=Object()						; Each segment is defined by its angle, segment 1,...,12 -> angle pi/6,pi/3,...,2*pi. (Unfortuantley its clockwise, with 0 being at three o'clock)
Loop,12
	segmentEndAngles[A_Index]:=pi/6*A_Index

; Display on screen visual aid for joystick control
Gui, Controller: New
Gui, Controller: +ToolWindow -Caption +AlwaysOnTop HWNDstick
Gui, Controller: Color, FFFFFF
Gui, Controller: Add, Picture, X0 Y0, circle.png						
Gui, Controller: Show,,Controller
WinSet,TransColor,FFFFFF,ahk_id %stick%				; Uncomment to make white transparent.
WinGetPos,WX,WY,,,ahk_id %stick%
	
; End autoexec.
	
; Hotkeys
#s::
	if toggle
	{
		SetTimer,mouse2joystick,%freq%
		MouseMove,OX,OY
		Sleep,10
		WinActivate, ahk_exe %gameExe%    ; You can comment this line if you don't want to activate the game when toggleing on the joystick
	}
	else
	{
		SetTimer,mouse2joystick,Off
	}
	toggle:=!toggle
return

#-::
	; Enable/Disable moving the control
	WinSet, Style, ^0xC00000, ahk_id %stick%
	WinGetPos,WX,WY,,,ahk_id %stick%	; Updates the circle center.
	OX:=100+WX								
	OY:=100+WY
return
ESC::ExitApp

; Labels

mouse2joystick:	
	mouse2joystick(r,ir,dr,OX,OY)
return

; Functions

mouse2joystick(r,ir,dr,OX,OY)
{
	; r is the radius of the outer circle
	; if is the radius of the inner circle
	; dr is a bounce back parameter.
	; OX is the x coord of circle center
	; OY is the y coord of circle center
	global actionTaken
	
	MouseGetPos,X,Y
	X-=OX										; Move to cirle coord system
	Y-=OY
	RR:=sqrt(X**2+Y**2)
	if (RR>r)									; Check if outside circle
		MouseMove,X*(r-dr)/RR+OX,Y*(r-dr)/RR+OY ; Calculate point on circle, move back to screen/window coords, and move mouse.
	if (RR>ir)									; Check if outside inner circle
	{
		; Calculate segement
		phi:=getAngle(X,Y)
		seg:=getSegment(phi)
		; Call action function.
		action(seg)
		actionTaken:=1							; This will enable fall back to center when leaving outer circle
	}
	else
	{
		Send,{W up}{D up}{S up}{A up}
		if (actionTaken=1)
		{
			MouseMove,OX,OY						; User has moved back to inner circle, fall back to center
			mouseBlock()						; Short mouse movmement block after fallback
			actionTaken:=0						; This will enable leaving the inner circle again.
		}
		;ToolTip,*	; Inside the inner circle, control is in default position
	}
	
}

action(seg)
{	
	; Define what to do, move the mouse around the circle to
	
	if (seg=1 || seg=12)
		Send,{D down}{W up}{S up}{A up}  
	else if (seg=2)
		Send,{D down}{S down}{W up}{A up}
	else if (seg=3 || seg= 4)
		Send,{S down}{W up}{D up}{A up}
	else if (seg=5)
		Send,{S down}{A down}{W up}{A up}
	else if (seg=6 || seg=7)
		Send,{A down}{W up}{D up}{S up}
	else if (seg=8)
		Send,{A down}{W down}{D up}{S up}
	else if (seg=9 || seg=10)
		Send,{W down}{D up}{S up}{A up}
	else if (seg=11)
		Send,{W down}{D down}{S up}{A up}
	else
		return -1 ; error
	return
}

getSegment(phi)
{
	global segmentEndAngles
	Loop 12
		if(phi<segmentEndAngles[A_Index])
			return A_Index
	return -1 ; error
}
getAngle(x,y)
{
	global pi
	x:=(x=0)?1:x 		; Avoid dividing with zero. No error is thrown it seems.
	phi:=atan(y/x)
	if (x<0 && y>0)
		return phi+pi
	if (x<0 && y<0)
		return phi+pi
	if (x>0 && y<0)
		return phi+2*pi
	return phi
}
mouseBlock()
{
	global fallBackPause
	BlockInput, MouseMove
	Sleep, %fallBackPause%
	BlockInput, MouseMoveOff
}
exitFunc()
{
	Send,{W up}{D up}{S up}{A up}
	ExitApp
}
; End
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

27 Jul 2016, 15:24

Sounds really cool mate.
I see a problem right away, line 38-39, move them to line 58.
Edit: Can't think of anything else that would cause that, line 168 should be changed to something like this though:

Code: Select all

x:=(x=0)?0.00001:x
This could actually cause getting in the wrong segment, I'm gonna see if I can get an error there.
Edit2; It's easy to get the error but it woudnt cause getting in the wrong segement due to the y variable be signifacntly larger, however if ir would be much smaller this could cause serious error. Better to correct it.
crisangelfan
Posts: 9
Joined: 24 Jul 2016, 20:13

Re: Mouse Movement to Controller stick/keyboard keys

31 Jul 2016, 23:37

Sorry for not responding to your post for a while, I've been away from my computer for a while and haven't messed with it since my last post. I'll try editing the script a bit and see what I can get.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Mouse Movement to Controller stick/keyboard keys

03 Aug 2016, 07:29

My MouseDelta library has a sample script which maps mouse to WSAD

MouseGetPos is fundamentally flawed for mouse input of this type, as it stops producing meaningful results if the mouse cursor hits the edge of the screen. My MouseDelta code is immune to this problem.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Mouse Movement to Controller stick/keyboard keys

04 Aug 2016, 12:06

I'll check it out later. As for mousegetpos, in my script the mouse is supposed to be placed over a gui window. But this was more of a toy-script, I'm sure there are plenty of free software if one would bother to google instead of taking the easy way out and making it from scratch. Cheers.

Return to “Gaming Scripts (v1)”

Who is online

Users browsing this forum: No registered users and 45 guests