Is Numpad Joystick Possible??? - AHK V1.1.32

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Gavnar
Posts: 7
Joined: 22 Apr 2020, 11:52

Is Numpad Joystick Possible??? - AHK V1.1.32

23 Apr 2020, 11:43

GREETINGS

I would like my keyboard numpad to fuction as a joystick and am not sure if it can be done in the way Id like it. I understand I will not have the variable speed of a joystick; pressing a button will cause the cursor to travel at a set speed unless of course I add a hotkey that speeds it up or slows it down etc, etc.

I have only been using AHK for 1 day and have since partly modified this very basic script (below) but I thought before I proceed to painstakingly scour the internet for days and weeks scraping together scraps of information here and there before I finally understand what Im doing and how to do it....... I should first find out if its even possible.

--------------------------------------------------------------------------------------------------------------

---SCRIPT---

Code: Select all

*Numpad8::MouseMove, 0, -10, 0, R  ; => Move cursor N
*Numpad2::MouseMove, 0, 10, 0, R  ; => Move cursor S
*Numpad4::MouseMove, -10, 0, 0, R  ; => Move cursor W
*Numpad6::MouseMove, 10, 0, 0, R  ; => Move cursor E
*Numpad7::MouseMove, -10, -10, 0, R  ; => Move cursor NW
*Numpad9::MouseMove, 10, -10, 0, R  ; => Move cursor NE
*Numpad1::MouseMove, -10, 10, 0, R  ; => Move cursor SW
*Numpad3::MouseMove, 10, 10, 0, R  ; => Move cursor SE

*<#RCtrl::  ; LeftWin + RightControl => Left-click (hold down Control/Shift to Control-Click or Shift-Click).
SendEvent {Blind}{LButton down}
KeyWait RCtrl  ; Prevents keyboard auto-repeat from repeating the mouse click.
SendEvent {Blind}{LButton up}
return

*<#AppsKey::  ; LeftWin + AppsKey => Right-click
SendEvent {Blind}{RButton down}
KeyWait AppsKey  ; Prevents keyboard auto-repeat from repeating the mouse click.
SendEvent {Blind}{RButton up}
return
---------------------------------------------------------------------------------------------------------------

These are the directions I would like and are written in compass format to avoid confusion:

8=N
6=E
2=S
4=W
7=NW
9=NE
3=SE
1=SW
7+4=WNW
7+8=NNW
9+8=NNE
9+6=ENE
3+6=ESE
3+2=SSE
1+2=SSW
1+4=WSW

- I would like the cursor movement to be silky smooth whether it be slow or fast, no jumping and jittering, no swishing across the screen forcing you to wait until it stops 'playing' like a movie. It should travel at a constant rate too from start to finish.
- When I press and hold 4 for instance it should respond instantly and only be moving because my finger is on the button. When I release it it should stop instantly. There should be no autorepeat delay after the intial button press and hold.
- If my finger is on 4 for example and I press and hold 8 as well it should not stop 4 then switch to 8; it should continue with 4 and initiate 8 as well combinding both directions effectively giving me a NW direction, and if I let go of 8 it should of course continue only with 4 so long as my finger is on the button. Although 7 is assisgned NW above it needs to be so so that when I press and hold both 7+8 for instance (if its possible) I get NNW giving me even finer control of my movement. Pressing any other non-directional key (a third perhaps like 5) should not interfere with the keys currently being pressed and vice versa, unless otherwise specified.

---------------------------------------------------------------------------------------------------------------

These points below are more a matter of interest/curiosity/nitpicking although since I am planning on using this for gaming too, point * would be a nice additional parameter if possible:

* I have experimented with another program which allowed smooth HIGH speed cursor movement with my keyboard however if I tapped and released a key quickly the cursor would intially jump or jolt in that direction and then from that point on proceeed to move smoothly. It wasnt very noticeable when pressing and holding the button down but a quick tap would cause a mini teleportation if you will. Is it possible to avoid this initial jump with this program? I would ideally like it to move differently based on how quickly the button is pressed and released, for example if I tap and release 4 quickly the cursor moves only a little even though the press and hold speed is high, similar to when youre playing a game and trying to get as close to an edge as possible without falling off as you tap tap tap the forward button. If I was playing a game with this 'joystick' numpad it would be usefull to have this feature when looking around to save me having to press another hotkey to reduce sensitivity/speed for small movements however I dont want it to interfere when Im trying to lock-on to a target by being laggy either. Perhaps Im asking for too much.

**I am interested in experimenting with different acceleration to decceleration rates. I like the idea of holding down numpad enter and then tapping and releasing 4 for example and the cursor in essence responds as though it were a flicked ball SO LONG AS numpad enter is held down. It could either 'roll' for a while but eventually slow down to a stop or continue on indefinitely based on how 'hard' it was flicked. By hard I mean if * above is possible a quick tap (nudge) would cause a subtle movement and a slightly longer harder press a flick/full movement. Releasing numpad enter would STOP ALL movement and pressing another direction whilst the cursor is moving would combine movements like expressed higher above. I get the feeling I have really overdone it with this one.

***I have noticed that when I press ctrl,alt,delete and go into task manager AHK stops working. Assuming that I am able to do all above, is there a way to get Windows to always recognise these 'commands'? I seem to recall reading something about a program that lets you alter your registry so that Windows takes these commands seriously. Will it work with simulated mouse/joystick movements too?

---------------------------------------------------------------------------------------------------------------

I have taken a long time to write this as clearly as I can. I would greatly appreciate it if you responded in at least a little more patience and clarity in you explainations than you normally would, as if explaining an Ipad to an elderly person lol. As stated above I only started using AHK yesterday and have much to learn. If you have some idea of what youre doing but are not 100% sure in some area please say so so that I know what to take to heart and what needs more input from another / learning on my behalf.

MANY THANKS
Last edited by Gavnar on 25 Apr 2020, 03:16, edited 1 time in total.
User avatar
Scr1pter
Posts: 1278
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: Is Numpad Joystick Possible??? - AHK V1.1.32

24 Apr 2020, 05:34

Hello and welcome,

You can try out this:

Code: Select all

#SingleInstance force
speedX = 20
speedY = 20

Numpad8::
while GetKeyState("Numpad8", "P") ; While key is being hold down
{
  MouseMove, 0, -speedY, 1, R
}
return

Numpad6::
while GetKeyState("Numpad6", "P") ; While key is being hold down
{
  MouseMove, speedX, 0, 1, R
}
return

Numpad2::
while GetKeyState("Numpad2", "P") ; While key is being hold down
{
  MouseMove, 0, speedY, 1, R
}
return

Numpad4::
while GetKeyState("Numpad4", "P") ; While key is being hold down
{
  MouseMove, -speedX, 0, 1, R
}
return

~Numpad8 & Numpad6::
~Numpad6 & Numpad8::
while GetKeyState("Numpad6", "P") ; While key is being hold down
{
  MouseMove, speedX, -speedY, 1, R
}
return
If you can live with that method, you can add the missing diagonal directions (NW, SW, SE) the same way as I did for NE.
Adjust the speedX and speedY if it's too fast or too slow.

Cheers!
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
Gavnar
Posts: 7
Joined: 22 Apr 2020, 11:52

Re: Is Numpad Joystick Possible??? - AHK V1.1.32

24 Apr 2020, 14:27

Thanks for the response, 44 views and only one response... I suppose my request does sound somewhat uninspiringly longwinded :) I have looked at your code and to be honest I only understand some of it but at least I can look up the commands and expand my knowledge on them, knowing WHAT to learn to achieve your goal is the biggest hurdle (I think) and you have pointed me in the right direction. Thanks very much. As lame as this sounds - I hope good things happen for you ;)
Gavnar
Posts: 7
Joined: 22 Apr 2020, 11:52

Re: Is Numpad Joystick Possible??? - AHK V1.1.32

29 Apr 2020, 12:31

Code: Select all

~Numpad8 & Numpad6::
~Numpad6 & Numpad8::
while GetKeyState("Numpad6", "P") ; While key is being hold down
{
  MouseMove, speedX, -speedY, 1, R
}
return
Ive noticed that although pressing num8 & num6 together gives the desired effect of causing the cursor to move NE; if Im holding num6 and then press num8 and then later let go of num8 to cancel NW direction it doesnt - it keeps going NW until I release num6??? Anyone know how to change that?

Thanx
User avatar
littlegandhi1199
Posts: 195
Joined: 29 Aug 2016, 23:58

Re: Is Numpad Joystick Possible??? - AHK V1.1.32

29 Apr 2020, 15:30

Gavnar wrote:
29 Apr 2020, 12:31

Code: Select all

~Numpad8 & Numpad6::
~Numpad6 & Numpad8::
while GetKeyState("Numpad6", "P") ; While key is being hold down
{
  MouseMove, speedX, -speedY, 1, R
}
return
Ive noticed that although pressing num8 & num6 together gives the desired effect of causing the cursor to move NE; if Im holding num6 and then press num8 and then later let go of num8 to cancel NW direction it doesnt - it keeps going NW until I release num6??? Anyone know how to change that?

Thanx
Instead of hotkeys you can always just build a loop that only listens to keystates and P stands for pressed btw

while GetKeyState("Numpad6", "P")

That's just a tip and to fix his code would have been easy man right?!
Once the hotkey was triggered it was only checking if Numpad6 was pressed.

You needed to only add in the 2nd condition that should have been true also (Numpad8)
And here's a simply way to add multiple statements together by putting them inside each other
If var1 = target
{
if var2 = target
{
msgbox, BOTH TRUE
}
}


Hotkeys will be very jolty and delays will occur when you switch from one hotkey to the next. All this stuff you will notice in 10 minutes of gameplay.
So scrap all that and build something to listen to the keys without hijacking anything and making a bunch of combination keybinds.
How you build that loop for listening only is to just check the keystates as often as you want. It'll never repeat and never lag/freeze between keys.
It's checking the keys at the %sleep% interval and each key only has to be written out once. Right adds positive X axis motion. Left adds negative.
Up adds negative Y axis motion. (up the pixels on the screen) and down adds positive Y axis motion.

If you press right and up it will add the correct motions for both independently and it's a simple math problem to handle X + or - ? and Y + - ?

You could even hold down all 4 keys at the same time and it wouldn't care. but + and - = 0 so direction would be = +0 X and +0 Y
No problem

Code: Select all

#SingleInstance force
#InstallKeybdHook
SetBatchLines, -1

;;;;;;;;;;;How long to sleep between each send of key (Milliseconds)
sleep = 10
;;;;;;;;;;;Adjust the smoothness...these two can affect each other (if the smoothness doesn't finish smoothing before the next command gets sent)
smooth = 1
;;;;;;;;;;;How far to go each command...could lower to give you more play room also
distX = 20
distY = 20

Loop
{
sleep, %sleep%
outX := 0
outY := 0


If (GetKeyState("Numpad8", "P"))
	outY -= distY
If (GetKeyState("Numpad5", "P"))			;;Down is set to Numpad5 (not 2) like your regular directional arrows would group them and your WASD keys would group them
	outY += distY							;;^^^ And you might even move them down to 1/2/3 and 5 so you could assign 0 to breaking/del to something and Enter too
If (GetKeyState("Numpad4", "P"))
	outX -= distX
If (GetKeyState("Numpad6", "P"))
	outX += distX

MouseMove, %outX%, %outY%, %smooth%, R
}


Numpad8::Return
Numpad5::Return
Numpad4::Return
Numpad6::Return
;;;;;;;;;;;;;;;^^^This part might not be necesary but it stops those keys from doing weird stuff because above it's not hijacking (it's only listening)
Script Backups on every Execution :mrgreen:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=75767&p=328155#p328155

Scrabble Solver 4-15 letter word outputs ( :crazy: # of inputs)
https://www.autohotkey.com/boards/viewtopic.php?f=19&t=34285
Gavnar
Posts: 7
Joined: 22 Apr 2020, 11:52

Re: Is Numpad Joystick Possible??? - AHK V1.1.32

02 May 2020, 10:44

Thanks very much for your time and help. Your way seems simpler yet more advanced. Ive been looking at AHK help page trying to full gaps in my understanding. The introduction section is very clear but as soon as you start looking up definitions of various terms the descriptions get shorter and assume youre a programmer who's trying to get a bit of code to finish their program, throwing terminology and jargon at you. Im going to have to scratch around the web for every single detail arent I? The beginner section had a progression to it...and then it ended; straight into the deep-end you go.

- Ive noticed that on my desktop it works perfectly yet in-game it flicks around at super speed all over the place, do you know why that is?
- Ive also noticed that diagonal movements are a lot faster than straight ones, I think because Im pressing 2 keys instead of 1 and the 'sleep = 10' or the speed setting for x and y is getting added together. Know how to fix this problem?
- There is also this strange effect after Ive been using the numpad. When using my actual mouse the cursor resists my hand movements (pushes back) as though I was still using the numpad and holding the opposite direction? It still does it from time to time very long after. I have to close the script to get rid of it even though its not in use. What is that?
User avatar
littlegandhi1199
Posts: 195
Joined: 29 Aug 2016, 23:58

Re: Is Numpad Joystick Possible??? - AHK V1.1.32

10 May 2020, 06:40

Gavnar wrote:
02 May 2020, 10:44
Thanks very much for your time and help. Your way seems simpler yet more advanced. Ive been looking at AHK help page trying to full gaps in my understanding. The introduction section is very clear but as soon as you start looking up definitions of various terms the descriptions get shorter and assume youre a programmer who's trying to get a bit of code to finish their program, throwing terminology and jargon at you. Im going to have to scratch around the web for every single detail arent I? The beginner section had a progression to it...and then it ended; straight into the deep-end you go.

- Ive noticed that on my desktop it works perfectly yet in-game it flicks around at super speed all over the place, do you know why that is?
- Ive also noticed that diagonal movements are a lot faster than straight ones, I think because Im pressing 2 keys instead of 1 and the 'sleep = 10' or the speed setting for x and y is getting added together. Know how to fix this problem?
- There is also this strange effect after Ive been using the numpad. When using my actual mouse the cursor resists my hand movements (pushes back) as though I was still using the numpad and holding the opposite direction? It still does it from time to time very long after. I have to close the script to get rid of it even though its not in use. What is that?
First off did you consider my suggestion to move your control keys down to 1 for left / 2-down/ 3-right/ 5-up so that you can assign 0 to a close button and Enter and Delete key also?
0
Old games have low resolution. distX and distY are referring to pixels and some old games have 800x600 (which goes fullscreen right?) so you need to lower your distance. Forget about how slow it is on your desktop that is not where you callibrate it.
Try lowering your distance until it's not too fast. (It'll probably be jerky though) so then adjust the smoothness and sleep time until it feels right...

Smoothness refers to how long it takes to move from your current position to the next one.
If the distance is high and the smoothness is high then it won't be finished sending the last command when the new one gets sent (and it will be ignored or queued up which is worse!)

sleep time is how often it sends a new mouse movement. Sending more (by lowering this value) will make it feel more smooth even if your smoothness is set to 0





I thought about diagnol speed but wanted to see if you cared or not. This should do it I think

Code: Select all

If ((outY != 0) && (outX != 0))
{
outY := outY/2
outX := outX/2
}
try adding that right before the mousemove command. Adjust it also if the ratio of speed isn't right. Maybe it should be 1.5 instead of 2 but I'm no mathmatician
Script Backups on every Execution :mrgreen:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=75767&p=328155#p328155

Scrabble Solver 4-15 letter word outputs ( :crazy: # of inputs)
https://www.autohotkey.com/boards/viewtopic.php?f=19&t=34285
Gavnar
Posts: 7
Joined: 22 Apr 2020, 11:52

Re: Is Numpad Joystick Possible??? - AHK V1.1.32

27 May 2020, 10:48

Ive considered it but still want the directions in my original post, I just think its a lot better to have that extra precision. I played around with smoothness and all it seems to do is add jitteryness when I hold down any direction button so Ive set it to 0, apart from that it strangely "smooths" my actual real mouse movements as well.

Ive been timing diagonal speeds and for example pressing num8 and num4 together DOES add up correctly and travel at the right speed (the outX and outY simply add together, no problems). I have turned all the extra numpad buttons also into diagonal buttons as well so I can for example either hold down num8+num4 or simply press num7 - both work exactly the same and at the same speed.

Code: Select all

#SingleInstance force
#InstallKeybdHook
SetBatchLines, -1

smooth = 0
sleep = -1

distX = 4
distY = 4

Loop
{
sleep, %sleep%
outX := 0
outY := 0

If (GetKeyState("Numpad8", "P"))
	outY -= distY
If (GetKeyState("Numpad2", "P"))			
	outY += distY							
If (GetKeyState("Numpad4", "P"))
	outX -= distX
If (GetKeyState("Numpad6", "P"))
	outX += distX
If (GetKeyState("Numpad7", "P"))
	outX -= distX, outY -= distY
If (GetKeyState("Numpad9", "P"))
	outX += distX, outY -= distY
If (GetKeyState("Numpad1", "P"))
	outX -= distX, outY += distY
If (GetKeyState("Numpad3", "P"))
	outX += distX, outY += distY

MouseMove, %outX%, %outY%, %smooth%, R
}

Numpad1::Return
Numpad2::Return
Numpad3::Return
Numpad4::Return
Numpad5::Return
Numpad6::Return
Numpad7::Return
Numpad8::Return
Numpad9::Return

^Esc::Pause
The main reason Ive done this though is not to have 2 ways of doing the same thing but because in order to get those finer diagonal movements that I specified above (NNE, ENE, ESE, SSE, SSW, WSW, WNW & NNW) I need to have 2 buttons to press to make it happen. The problem however is that when I do so the movement is a lot faster than it should be. I think its because for example num8 = outY - & num7 = BOTH outX -, AND outY - so there 2 outY - being combined increasing the speed; well thats my theory anyway. I tried being clever by adding the code:

Code: Select all

If (GetKeyState("Numpad8 & Numpad7", "P"))
	outX -= distX, outY -= distY/2
No errors came up though this didnt seem to do anything. It seems to be just ignored. The problem seems to be the dedicated diagonal buttons (num7,9,3,&1). Is there a way to cancel or refresh them when another direction button is pressed (without creating new problems/bugs)?
User avatar
littlegandhi1199
Posts: 195
Joined: 29 Aug 2016, 23:58

Re: Is Numpad Joystick Possible??? - AHK V1.1.32

30 May 2020, 13:25

Alright man. I get what you mean.
When you don't know the function just look it up in your manual. Open the autohotkey program (not a script) from the search menu or whatever opens the full list of commands. Then go to the index tab and type it in. Then you should have looked @ GetKeyState to see that it only accepts 1 key and not two like you tried.

Maybe you didn't know how to do IF AND statements here's how.
If ((var = 1) && (var2 = 1))
something

The syntax just gets a little confusing to look at with functions that have their own () brackets
If ((GetKeyState("Numpad8", "P")) && (GetKeyState("Numpad7", "P")))

So I've done half of them for you and also made it check those all first. Then if none are true it runs each key individually as the original code intended.

Keep going in that format and remember that there are two versions of NNW for example...
There is the way where you move North quicker (aka Y*2) and then there is the way you described moving West at half speed (aka X/2)

You should try both. See which one you want.

Code: Select all

If A_IsCompiled != 1
	Menu, Tray, Icon, C:\Program Files\AutoHotkey\AutoHotkey.exe, 2

#SingleInstance force
#InstallKeybdHook
SetBatchLines, -1

smooth = 0
sleep = -1

distX = 4
distY = 4

Loop
{
sleep, %sleep%
outX := 0
outY := 0


;;NNE, ENE, ESE, SSE, SSW, WSW, WNW & NNW


If ((GetKeyState("Numpad8", "P")) && (GetKeyState("Numpad7", "P")))				;;NNW
	outX -= distX/2, outY -= distY
Else If ((GetKeyState("Numpad4", "P")) && (GetKeyState("Numpad7", "P")))		;;WNW
	outX -= distX, outY -= distY/2
Else If ((GetKeyState("Numpad4", "P")) && (GetKeyState("Numpad1", "P")))		;;WSW
	outX -= distX, outY += distY/2
Else If ((GetKeyState("Numpad2", "P")) && (GetKeyState("Numpad1", "P")))		;;SSW
	outX -= distX/2, outY += distY
Else
{
If (GetKeyState("Numpad8", "P"))
	outY -= distY
If (GetKeyState("Numpad2", "P"))			
	outY += distY							
If (GetKeyState("Numpad4", "P"))
	outX -= distX
If (GetKeyState("Numpad6", "P"))
	outX += distX
If (GetKeyState("Numpad7", "P"))
	outX -= distX, outY -= distY
If (GetKeyState("Numpad9", "P"))
	outX += distX, outY -= distY
If (GetKeyState("Numpad1", "P"))
	outX -= distX, outY += distY
If (GetKeyState("Numpad3", "P"))
	outX += distX, outY += distY
}

MouseMove, %outX%, %outY%, %smooth%, R
}

Numpad1::Return
Numpad2::Return
Numpad3::Return
Numpad4::Return
Numpad5::Return
Numpad6::Return
Numpad7::Return
Numpad8::Return
Numpad9::Return

^Esc::Pause
Script Backups on every Execution :mrgreen:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=75767&p=328155#p328155

Scrabble Solver 4-15 letter word outputs ( :crazy: # of inputs)
https://www.autohotkey.com/boards/viewtopic.php?f=19&t=34285

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, Hansielein, Lpanatt and 323 guests