AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Multiple Joystick to Keyboard Mapping (JoyToKey)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
jaxx0rr
Guest





PostPosted: Wed Jun 03, 2009 9:19 am    Post subject: Multiple Joystick to Keyboard Mapping (JoyToKey) Reply with quote

A small script with customizable keys for 2 gamepads that translate to a keyboard, simply run it, enter the game and configure your joystick instead of keyboard in any game..

If you have more than 2 gamepads or joysticks simply change the numbers at the beginning of the code and test it until it works

Also you can change what keys are pressed although it does not matter if you set them in the game interface directly with the gamepad

and here is the code..

Code:

;created by jaxx0rr
;pov segment is by another author ( not sure who )

#Persistent
SetFormat, float, 03
SetTimer, WatchJoysticks, 5
return

WatchJoysticks:

;=======================================================================================
;=======================================================================================
;========    SETTINGS    ===============================================================
;=======================================================================================
;=======================================================================================

;set your joystick %1 (type in different numbers and test joysticks to see whats what)
JoystickNumber1 = 1

;set the keys for the first joystick (this will be the pow)
JoystickNumber1KeyUp = Up
JoystickNumber1KeyDown = Down
JoystickNumber1KeyLeft = Left
JoystickNumber1KeyRight = Right
JoystickNumber1Btn1 = 1
JoystickNumber1Btn2 = 2
JoystickNumber1Btn3 = 3
JoystickNumber1Btn4 = 4
JoystickNumber1Btn5 = 5
JoystickNumber1Btn6 = 6
JoystickNumber1Btn7 = 7
JoystickNumber1Btn8 = 8
JoystickNumber1Btn9 = 9
JoystickNumber1Btn10 = 0
JoystickNumber1Btn11 = q
JoystickNumber1Btn12 = e

;set your joystick %2 (type in different numbers and test joysticks to see whats what)
JoystickNumber2 = 2

;set the keys for the second joystick (this will be the pow)
JoystickNumber2KeyUp = w
JoystickNumber2KeyDown = s
JoystickNumber2KeyLeft = a
JoystickNumber2KeyRight = d
JoystickNumber2Btn1 = z
JoystickNumber2Btn2 = x
JoystickNumber2Btn3 = c
JoystickNumber2Btn4 = v
JoystickNumber2Btn5 = b
JoystickNumber2Btn6 = n
JoystickNumber2Btn7 = m
JoystickNumber2Btn8 = f
JoystickNumber2Btn9 = g
JoystickNumber2Btn10 = h
JoystickNumber2Btn11 = j
JoystickNumber2Btn12 = k


;=======================================================================================
;=======================================================================================
;========    END OF SETTINGS    ======= dont edit below this unless ur a pro ... =======
;=======================================================================================
;=======================================================================================









;===================================================================================
;==== JOYPAD 1 =====================================================================
;===================================================================================


;axis to direction
;=======================================================================================

GetKeyState, joyx, %JoystickNumber1%JoyX
GetKeyState, joyy, %JoystickNumber1%JoyY

AXY1Key1ToHoldDownPrev = %AXY1Key1ToHoldDown%
AXY1Key2ToHoldDownPrev = %AXY1Key2ToHoldDown%



if joyx < 20
{
   if joyy < 20
   {
      AXY1Key1ToHoldDown = %JoystickNumber1KeyUp%
      AXY1Key2ToHoldDown = %JoystickNumber1KeyLeft%
   }
   else if joyy between 40 and 60
   {
      AXY1Key1ToHoldDown =
      AXY1Key2ToHoldDown = %JoystickNumber1KeyLeft%
   }
   else if joyy > 80
   {
      AXY1Key1ToHoldDown = %JoystickNumber1KeyDown%
      AXY1Key2ToHoldDown = %JoystickNumber1KeyLeft%
   }
}
else if joyx  between 40 and 60
{
   if joyy < 20
   {
      AXY1Key1ToHoldDown = %JoystickNumber1KeyUp%
      AXY1Key2ToHoldDown =
   }
   else if joyy  between 40 and 60
   {
      AXY1Key1ToHoldDown =
      AXY1Key2ToHoldDown =
   }
   else if joyy > 80
   {
      AXY1Key1ToHoldDown = %JoystickNumber1KeyDown%
      AXY1Key2ToHoldDown =
   }
}
else if joyx > 80
{
   if joyy < 20
   {
      AXY1Key1ToHoldDown = %JoystickNumber1KeyUp%
      AXY1Key2ToHoldDown = %JoystickNumber1KeyRight%
   }
   else if joyy  between 40 and 60
   {
      AXY1Key1ToHoldDown =
      AXY1Key2ToHoldDown = %JoystickNumber1KeyRight%
   }
   else if joyy > 80
   {
      AXY1Key1ToHoldDown = %JoystickNumber1KeyDown%
      AXY1Key2ToHoldDown = %JoystickNumber1KeyRight%
   }
}

;ToolTip, %joyx% - %joyy% - %AXY1Key1ToHoldDown% - %AXY1Key2ToHoldDown%

SetKeyDelay -1 

if AXY1Key1ToHoldDown <> %AXY1Key1ToHoldDownPrev%
{
   if AXY1Key1ToHoldDownPrev <>
      Send, {%AXY1Key1ToHoldDownPrev% up}
   if AXY1Key1ToHoldDown <>
      Send, {%AXY1Key1ToHoldDown% down}
}
if AXY1Key2ToHoldDown <> %AXY1Key2ToHoldDownPrev%
{
   if AXY1Key2ToHoldDownPrev <>
      Send, {%AXY1Key2ToHoldDownPrev% up}
   if AXY1Key2ToHoldDown <>
      Send, {%AXY1Key2ToHoldDown% down}
}





;buttons
;=======================================================================================



GetKeyState, joy_buttons, %JoystickNumber1%JoyButtons
Loop, %joy_buttons%
{
   GetKeyState, joy%a_index%, %JoystickNumber1%joy%a_index%
   if joy%a_index% = D
   {
      Value := JoystickNumber1Btn%a_index%
      Send, {%Value% down}
      JoystickNumber1BtnState%a_index% = dn
   }
   else
   {
      if JoystickNumber1BtnState%a_index% = dn
      {
         Value := JoystickNumber1Btn%a_index%
         Send, {%Value% up}
      }
      JoystickNumber1BtnState%a_index% = up
   }
}




;povs to direction
;=======================================================================================



GetKeyState, POV, %JoystickNumber1%JoyPOV

POV1Key1ToHoldDownPrev = %POV1Key1ToHoldDown%
POV1Key2ToHoldDownPrev = %POV1Key2ToHoldDown%

if POV < 0
{
   POV1Key1ToHoldDown =
   POV1Key2ToHoldDown =
}
else if POV > 33750
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyUp%
   POV1Key2ToHoldDown =
}
else if POV between 0 and 2250
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyUp%
   POV1Key2ToHoldDown =
}
else if POV between 2251 and 6750
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyUp%
   POV1Key2ToHoldDown = %JoystickNumber1KeyRight%
}
else if POV between 6751 and 11250
{
   POV1Key1ToHoldDown =
   POV1Key2ToHoldDown = %JoystickNumber1KeyRight%
}
else if POV between 11251 and 15750
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyUp%
   POV1Key2ToHoldDown = %JoystickNumber1KeyRight%
}
else if POV between 15751 and 20250
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyDown%
   POV1Key2ToHoldDown =
}
else if POV between 20251 and 24750
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyDown%
   POV1Key2ToHoldDown = %JoystickNumber1KeyLeft%
}
else if POV between 24751 and 29250
{
   POV1Key1ToHoldDown =
   POV1Key2ToHoldDown = %JoystickNumber1KeyLeft%
}
else if POV between 29251 and 33750
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyUp%
   POV1Key2ToHoldDown = %JoystickNumber1KeyLeft%
}

SetKeyDelay -1

if POV1Key1ToHoldDown <> %POV1Key1ToHoldDownPrev%
{
   if POV1Key1ToHoldDownPrev <>
      Send, {%POV1Key1ToHoldDownPrev% up}
   if POV1Key1ToHoldDown <>
      Send, {%POV1Key1ToHoldDown% down}
}
if POV1Key2ToHoldDown <> %POV1Key2ToHoldDownPrev%
{
   if POV1Key2ToHoldDownPrev <>
      Send, {%POV1Key2ToHoldDownPrev% up}
   if POV1Key2ToHoldDown <>
      Send, {%POV1Key2ToHoldDown% down}
}




;===================================================================================
;==== JOYPAD 2 =====================================================================
;===================================================================================




;axis to direction
;=======================================================================================

GetKeyState, joyx, %JoystickNumber2%JoyX
GetKeyState, joyy, %JoystickNumber2%JoyY

AXY2Key1ToHoldDownPrev = %AXY2Key1ToHoldDown%
AXY2Key2ToHoldDownPrev = %AXY2Key2ToHoldDown%



if joyx < 20
{
   if joyy < 20
   {
      AXY2Key1ToHoldDown = %JoystickNumber2KeyUp%
      AXY2Key2ToHoldDown = %JoystickNumber2KeyLeft%
   }
   else if joyy between 40 and 60
   {
      AXY2Key1ToHoldDown =
      AXY2Key2ToHoldDown = %JoystickNumber2KeyLeft%
   }
   else if joyy > 80
   {
      AXY2Key1ToHoldDown = %JoystickNumber2KeyDown%
      AXY2Key2ToHoldDown = %JoystickNumber2KeyLeft%
   }
}
else if joyx between 40 and 60
{
   if joyy < 20
   {
      AXY2Key1ToHoldDown = %JoystickNumber2KeyUp%
      AXY2Key2ToHoldDown =
   }
   else if joyy between 40 and 60
   {
      AXY2Key1ToHoldDown =
      AXY2Key2ToHoldDown =
   }
   else if joyy > 80
   {
      AXY2Key1ToHoldDown = %JoystickNumber2KeyDown%
      AXY2Key2ToHoldDown =
   }
}
else if joyx > 80
{
   if joyy < 20
   {
      AXY2Key1ToHoldDown = %JoystickNumber2KeyUp%
      AXY2Key2ToHoldDown = %JoystickNumber2KeyRight%
   }
   else if joyy between 40 and 60
   {
      AXY2Key1ToHoldDown =
      AXY2Key2ToHoldDown = %JoystickNumber2KeyRight%
   }
   else if joyy > 80
   {
      AXY2Key1ToHoldDown = %JoystickNumber2KeyDown%
      AXY2Key2ToHoldDown = %JoystickNumber2KeyRight%
   }
}


SetKeyDelay -1 

if AXY2Key1ToHoldDown <> %AXY2Key1ToHoldDownPrev% 
{
   if AXY2Key1ToHoldDownPrev <>   
      Send, {%AXY2Key1ToHoldDownPrev% up} 
   if AXY2Key1ToHoldDown <> 
      Send, {%AXY2Key1ToHoldDown% down} 
}
if AXY2Key2ToHoldDown <> %AXY2Key2ToHoldDownPrev% 
{
   if AXY2Key2ToHoldDownPrev <>   
      Send, {%AXY2Key2ToHoldDownPrev% up} 
   if AXY2Key2ToHoldDown <> 
      Send, {%AXY2Key2ToHoldDown% down} 
}





;buttons
;=======================================================================================

GetKeyState, joy_buttons, %JoystickNumber2%JoyButtons
Loop, %joy_buttons%
{
   GetKeyState, joy%a_index%, %JoystickNumber2%joy%a_index%
   if joy%a_index% = D
   {
      Value := JoystickNumber2Btn%a_index%
      Send, {%Value% down}
      JoystickNumber2BtnState%a_index% = dn
   }
   else
   {
      if JoystickNumber2BtnState%a_index% = dn
      {
         Value := JoystickNumber2Btn%a_index%
         Send, {%Value% up}
      }
      JoystickNumber2BtnState%a_index% = up
   }
}




;povs to direction
;=======================================================================================


GetKeyState, POV, %JoystickNumber2%JoyPOV

POV2Key1ToHoldDownPrev = %POV2Key1ToHoldDown%
POV2Key2ToHoldDownPrev = %POV2Key2ToHoldDown%

if POV < 0
{
   POV2Key1ToHoldDown =
   POV2Key2ToHoldDown =
}
else if POV > 33750
{
   POV2Key1ToHoldDown = %JoystickNumber2KeyUp%
   POV2Key2ToHoldDown =
}
else if POV between 0 and 2250
{
   POV2Key1ToHoldDown = %JoystickNumber2KeyUp%
   POV2Key2ToHoldDown =
}
else if POV between 2251 and 6750
{
   POV2Key1ToHoldDown = %JoystickNumber2KeyUp%
   POV2Key2ToHoldDown = %JoystickNumber2KeyRight%
}
else if POV between 6751 and 11250
{
   POV2Key1ToHoldDown =
   POV2Key2ToHoldDown = %JoystickNumber2KeyRight%
}
else if POV between 11251 and 15750
{
   POV2Key1ToHoldDown = %JoystickNumber2KeyUp%
   POV2Key2ToHoldDown = %JoystickNumber2KeyRight%
}
else if POV between 15751 and 20250
{
   POV2Key1ToHoldDown = %JoystickNumber2KeyDown%
   POV2Key2ToHoldDown =
}
else if POV between 20251 and 24750
{
   POV2Key1ToHoldDown = %JoystickNumber2KeyDown%
   POV2Key2ToHoldDown = %JoystickNumber2KeyLeft%
}
else if POV between 24751 and 29250
{
   POV2Key1ToHoldDown =
   POV2Key2ToHoldDown = %JoystickNumber2KeyLeft%
}
else if POV between 29251 and 33750
{
   POV2Key1ToHoldDown = %JoystickNumber2KeyUp%
   POV2Key2ToHoldDown = %JoystickNumber2KeyLeft%
}

SetKeyDelay -1

if POV2Key1ToHoldDown <> %POV2Key1ToHoldDownPrev%
{
   if POV2Key1ToHoldDownPrev <>
      Send, {%POV2Key1ToHoldDownPrev% up}
   if POV2Key1ToHoldDown <>
      Send, {%POV2Key1ToHoldDown% down}
}
if POV2Key2ToHoldDown <> %POV2Key2ToHoldDownPrev%
{
   if POV2Key2ToHoldDownPrev <>
      Send, {%POV2Key2ToHoldDownPrev% up}
   if POV2Key2ToHoldDown <>
      Send, {%POV2Key2ToHoldDown% down}
}




return


cheers
Back to top
Rotta
Guest





PostPosted: Sat Jul 04, 2009 1:03 pm    Post subject: Reply with quote

Nice script, only it has a problem with POV code at about line 240 for joypad1 and the same mistake at way down for joypad2:

Code:
else if POV between 11251 and 15750
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyUp%
   POV1Key2ToHoldDown = %JoystickNumber1KeyRight%
}


should be:

Code:
else if POV between 11251 and 15750
{
   POV1Key1ToHoldDown = %JoystickNumber1KeyDown%
   POV1Key2ToHoldDown = %JoystickNumber1KeyRight%
}



Cheers
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group