Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Kind of rotary switch with a joystick axis


  • Please log in to reply
19 replies to this topic
libory
  • Members
  • 2 posts
  • Last active: May 09 2009 10:50 PM
  • Joined: 08 May 2009
Hello, does anybody have a kind of script to do this more or less:

Let's say I have X axis 0 to 100. I would like to divide the axis let's say in 4 parts and if I turn right in every of those 4 steps send a key like "a" and if I turn left the axis again in those 4 steps send a key like "b".

I m trying to make a rotary switch of two keys with a joystick axis....

Thanks a lot I hope some one has a nice script for it....

[Moved from Scripts & Functions forum. ~jaco0646]

anyb0dy
  • Guests
  • Last active:
  • Joined: --
Sorry. No. :(

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007
Could you explain that more clearly please? Are you trying to simulate keystrokes using joystick input, or simulate a joystick using keyboard input? AutoHotkey can do the first but not the second.

libory
  • Members
  • 2 posts
  • Last active: May 09 2009 10:50 PM
  • Joined: 08 May 2009
I would like to send keystrokes from a joystick input. I have a usb board with 8 axis and 32 buttons. By the way... is this software (autohotkey) only accepts 6 axis?

I have a homemade cockpit or flight simulator and those axis are linked to knobs (like a volume knob in a stereo).

The thing I m trying to do is emulate keystrokes with the turn of these knobs. If it would be one key only it would be easy but the problem I have is how to detect in wich direction turns.....

For example:

If I turn right the knob (X axis) once reaches at 20% send "a" keystroke, 40% another "a" and so on but if I rotate the knob to the left if it reaches 40% again then it would send a "b" keystroke.

I have done this with other software but instead of using pots I used a rotary switches with resistors to emulate the pot and it workded...

Thanks for your answer...

garath
  • Members
  • 398 posts
  • Last active: Feb 05 2011 10:31 PM
  • Joined: 24 Mar 2005
Did you read the documentation about Getkeystate?
http://www.autohotke...GetKeyState.htm
And did you try the Joystick-TestScript?
http://www.autohotke...oystickTest.htm
And did you read the part about Joystick remapping?
http://www.autohotke...mapJoystick.htm

You could read the State of your Joystick continuously and send the proper keys.

Send us a code, you are trying to work with, then we can help you to adapt it to your needs.
I think, it is possible to remap your Jostick in the way you want it, at least the first 6 axis.

flyway
  • Guests
  • Last active:
  • Joined: --
Hey there,
Here's a solution. It worked for me.
Axis just need to be rotated slowly, couldn't manage so far to control great axis rotation speeds.

Just count how many keystrokes are needed to rotate that knob from one end to the other and divide 100 (full axis travel) from that number of keystrokes needed.

Soon will implement Audio knobs and will post my code.

If someone with more script knowledge can write a better code for this, including some code lines to take care of faster axis rotation, it would be very appreciated.

#Persistent  ; Keep this script running until the user explicitly exits it.
SetTimer, WatchAxis, 5
return


WatchAxis:
GetKeyState, 2JoyV, 2JoyV  ; Get position of Xrotation axis.
KeyToHoldDownPrev = %KeyToHoldDown%  ; Prev now holds the key that was down before (if any).


If (round(2JoyV) <> 3 
and round(2JoyV) <> 6 
and round(2JoyV) <> 9 
and round(2JoyV) <> 12 
and round(2JoyV) <> 15 
and round(2JoyV) <> 18 
and round(2JoyV) <> 21 
and round(2JoyV) <> 24 
and round(2JoyV) <> 27 
and round(2JoyV) <> 30 
and round(2JoyV) <> 33 
and round(2JoyV) <> 36 
and round(2JoyV) <> 39 
and round(2JoyV) <> 42 
and round(2JoyV) <> 45 
and round(2JoyV) <> 48 
and round(2JoyV) <> 51 
and round(2JoyV) <> 54 
and round(2JoyV) <> 57 
and round(2JoyV) <> 60 
and round(2JoyV) <> 63 
and round(2JoyV) <> 66 
and round(2JoyV) <> 69 
and round(2JoyV) <> 71 
and round(2JoyV) <> 74 
and round(2JoyV) <> 77 
and round(2JoyV) <> 80 
and round(2JoyV) <> 83 
and round(2JoyV) <> 86 
and round(2JoyV) <> 89 
and round(2JoyV) <> 92 
and round(2JoyV) <> 95
and round(2JoyV) <> 98)
	return

ValNEW := round(2JoyV) - ValINI
If (ValNEW < 0)
	Send, !+{Insert}

else If (ValNEW > 0)
	Send, !+{PgUp}

ValINI := round(2JoyV)

return


Morpheus
  • Members
  • 475 posts
  • Last active: Oct 21 2014 11:08 AM
  • Joined: 31 Jul 2008
It looks like you are checking for multiples of three, but you skipped from 69 to 71?

If you are trying to check if the value is a multiple of 3, I think it would be faster to divide by three, then use If var is [not] type to check if it is an integer or not.

VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
VUpKey = a

VDownKey = b

Axis = V ; these are the axises we're using

NumberOfRanges = 4 ; how many 'steps' do we want the axis to have between 0 and 100

JoyNum = ; put a number if you have more than 1 joystick

; WARNING: no newbies below this line

#Persistent

SendMode, Input ; es muy rapido

SetTimer, WatchAxis, 10

WatchAxis:

Loop, Parse, Axis ; Sets the variable A_LoopField to the current axis

If (p%A_LoopField% != j%A_LoopField% := 1 + (GetKeyState(joynum 

. "Joy" . A_LoopField, "P") * NumberOfRanges - 1 ) // 100)

{

   If (p%A_LoopField% != "")

      Send % "{Blind}{" . ((p%A_LoopField% < j%A_LoopField%)

      ? %A_LoopField%UpKey : %A_LoopField%DownKey) . "}"

   p%A_LoopField% := j%A_LoopField%

}

return
:?:

flyway
  • Guests
  • Last active:
  • Joined: --
WOW VxE... :shock: :shock: :shock:

Thanks a big bunch.
Will try it soon.

  • Guests
  • Last active:
  • Joined: --

...
If you are trying to check if the value is a multiple of 3, I think it would be faster to divide by three, then use If var is [not] type to check if it is an integer or not.


Yep, because Yaw Trim axis needs 31 (up or down) Keystrokes to travel fully, end to end. So 100 / 31 ~ 3º per keystroke instance.

flyway
  • Guests
  • Last active:
  • Joined: --
Sorry about my last post, forgot to put my username.. lol.

Now, [VxE]

Have tried your code with this modifications, the basics;
VUpKey = !+{Insert} ; SHF+ALT+INSERT
VDownKey = !+{PgUp} ; SHF+ALT+PGUP
Axis = V ; these are the axises we're using
NumberOfRanges = 3 ; how many 'steps' do we want the axis to have between 0 and 100
JoyNum = 2 ; put a number if you have more than 1 joystick
; WARNING: no newbies below this line
#Persistent
SendMode, Input ; es muy rapido
SetTimer, WatchAxis, 10
WatchAxis:
Loop, Parse, Axis ; Sets the variable A_LoopField to the current axis
If (p%A_LoopField% != j%A_LoopField% := 1 + (GetKeyState(joynum
. "Joy" . A_LoopField, "P") * NumberOfRanges - 1 ) // 100)
{
   If (p%A_LoopField% != "")
      Send % "{Blind}{" . ((p%A_LoopField% < j%A_LoopField%)
      ? %A_LoopField%UpKey : %A_LoopField%DownKey) . "}"
   p%A_LoopField% := j%A_LoopField%
}
return

And it was a bit strange, but I think it work at first, or maybe I forgot to load that code into AutoKey.. lol.
Anyway, now it doesn't work for those chosen keystrokes. If I let "a" and "b" there it works.
Any Ideas ?

Thanks.

VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
Send % "{Blind}[color=red]{[/color]" . ((p%A_LoopField% < j%A_LoopField%)
      ? %A_LoopField%UpKey : %A_LoopField%DownKey) . "[color=red]}[/color]"
The {braces} are what's making it mess up with those particular keys. Remove them and it should be fine.

flyway
  • Guests
  • Last active:
  • Joined: --

Send % "{Blind}[color=red]{[/color]" . ((p%A_LoopField% < j%A_LoopField%)
      ? %A_LoopField%UpKey : %A_LoopField%DownKey) . "[color=red]}[/color]"
The {braces} are what's making it mess up with those particular keys. Remove them and it should be fine.


Thanks again bud.
Will try it tonight.

One more quest.
Will need to add more axis to this code, using different keystrokes, diff. axis (sure...lol) and diff. NumberOfRanges. Now, my knowledge tells me that I can append new keystrokes to send bellow actual ones, new axis definition bellow actual one and probably renaming NumberOfRanges to VNumberOfRanges in order to append the new ones bellow it.
But what about the Loop...?
Can I append new Loops bellow "Return" command ?

Thanks,

Cheers.

VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
Actually, the original code was designed to support any number of axes (look at what the code does with the variable Axis). Here's an example of 2 axes: (additional changes marked in red)
VUpKey = !+{Insert} ; SHF+ALT+INSERT

VDownKey = !+{PgUp} ; SHF+ALT+PGUP

VNumberOfRanges = 3 ; how many 'steps' do we want the V axis to have between 0 and 100



UUpKey = +{Right} ; SHF+RIGHT

UDownKey = +{Left} ; SHF+LEFT

UNumberOfRanges = 21 ; how many 'steps' do we want the U axis to have between 0 and 100



Axis = [color=red]U[/color]V ; these are the axises we're using

JoyNum = 2 ; put a number if you have more than 1 joystick

; WARNING: no newbies below this line

#Persistent

SendMode, Input ; es muy rapido

SetTimer, WatchAxis, 10

WatchAxis:

Loop, Parse, Axis ; Sets the variable A_LoopField to the current axis

If (p%A_LoopField% != j%A_LoopField% := 1 + (GetKeyState(joynum

. "Joy" . A_LoopField, "P") * [color=red]%A_LoopField%[/color]NumberOfRanges - 1 ) // 100)

{

   If (p%A_LoopField% != "")

      Send % "{Blind}" . ((p%A_LoopField% < j%A_LoopField%)

      ? %A_LoopField%UpKey : %A_LoopField%DownKey) . ""

   p%A_LoopField% := j%A_LoopField%

}

return


flyway
  • Guests
  • Last active:
  • Joined: --
WOW, thanks alot my friend.
And sorry for all these efforts.
Yesterday it worked like a charm.
Tonight will check this multi axis version.
So, I can proceed with this logic for any number of axis, I presume.
One other thing that I tried to implement and didn't work is a limitation regarding axis range, ex:

I wanted to disregard both ending 10% of axis, see?
And also this must vary with the axis, because YawTrims only rotate 180ºdegrees, but audio axis may rotate 190º till 210º...
So I tried to put this right after Loop call;

If A_LoopField < 5 or A_LoopField > 95
Return

And it didn't work... :lol:

Thanks again.