Mouse Movement to Controller stick/keyboard keys
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Mouse Movement to Controller stick/keyboard keys
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
Re: Mouse Movement to Controller stick/keyboard keys
If I have time tonight I'll make something, shouldn't be a problem.
Re: Mouse Movement to Controller stick/keyboard keys
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:
Put image in same folder as script, run.
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
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Re: Mouse Movement to Controller stick/keyboard keys
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.
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.
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Re: Mouse Movement to Controller stick/keyboard keys
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
Re: Mouse Movement to Controller stick/keyboard keys
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.
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Re: Mouse Movement to Controller stick/keyboard keys
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.
Re: Mouse Movement to Controller stick/keyboard keys
If you post your code I can look at it.
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Re: Mouse Movement to Controller stick/keyboard keys
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
Re: Mouse Movement to Controller stick/keyboard keys
Ok, i'll take a look.
Re: Mouse Movement to Controller stick/keyboard keys
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:
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
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Re: Mouse Movement to Controller stick/keyboard keys
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.
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.
Re: Mouse Movement to Controller stick/keyboard keys
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.
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.
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Re: Mouse Movement to Controller stick/keyboard keys
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.
Re: Mouse Movement to Controller stick/keyboard keys
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..
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..
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Re: Mouse Movement to Controller stick/keyboard keys
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:
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
Re: Mouse Movement to Controller stick/keyboard keys
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: 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.
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
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.
-
- Posts: 9
- Joined: 24 Jul 2016, 20:13
Re: Mouse Movement to Controller stick/keyboard keys
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.
Re: Mouse Movement to Controller stick/keyboard keys
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.
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.
Re: Mouse Movement to Controller stick/keyboard keys
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 50 guests