AutoHotkey Community

It is currently May 27th, 2012, 12:02 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Joystick Driver
PostPosted: December 2nd, 2005, 12:17 am 
Offline

Joined: November 29th, 2005, 7:07 am
Posts: 5
I have created a basic interface driver for my PS2 joystick for Warcraft. Yet the problem is that NONE of my modifier keys are operation while AHK is running and my script is loaded. Did i break something with this code. I started with the code provided in the documentation. Thank you in advance for such an amazing product.

Code:
; Using a PS2 Gamepad Keyboard Mapper
; Made by NecriShiite
;
; Designed for World Of Warcraft
;
; This script specially maps the PS2 Gamepad to the keyboard.
; It remaps each button and joystick to the keyboard. It also
; uses virtually no CPU time.  It is recommended to customize
; each of the settings in the config section for your needs.

; ========================BEGIN OF CONFIG SECTION=============================

; Increase the following value to make the mouse cursor move faster:
JoyMultiplier = 0.40

; Decrease the following value to require less joystick displacement-from-center
; to start moving the mouse.  However, you may need to calibrate your joystick
; -- ensuring it's properly centered -- to avoid cursor drift. A perfectly tight
; and centered joystick could use a value of 1:
JoyThreshold = 10

; Change these values to use joystick button numbers other than 1, 2, and 3 for
; the left, right, and middle mouse buttons, respectively:
BtnSquare = 4
BtnCross = 3
BtnCircle = 2
BtnTriangle = 1
BtnL2 = 5
BtnR2 = 6
BtnL1 = 7
BtnR1 = 8


; Change these values to specify the newly mapped key you want to use for
; each previously defined button above
SquareKey = Enter
CrossKey = .
CircleKey = Space
TriangleKey = Escape
;R1Key =
;R2Key =
;L1Key =
;L2Key =

; Change these values to specify the newly mapped key you want to use
; the Left joystick on the PS2 Gamepad
JoyLeftKey = Left
JoyRightKey = Right
JoyUpKey = Up
JoyDownKey = Down

; If your joystick has a POV control, you can use it as a mouse wheel.  The
; following value is the number of milliseconds between turns of the wheel.
; Decrease it to have the wheel turn faster:
WheelDelay = 100

; If there are multiple joysticks, change this value to another joystick ID
; Look under gaming options in the Control Panel for the correct ID
JoyNum = 1

; Calculate the axis displacements that are needed to start moving the cursor:
JoyUpper = 50
JoyUpper += %JoyThreshold%
JoyLower = 50
JoyLower -= %JoyThreshold%

; =======================END OF CONFIG SECTION================================




#SingleInstance
SetFormat, float, 03  ; Omit decimal points from numbers
GetKeyState, joy_name, %JoyNum%JoyName

Hotkey, %JoyNum%Joy%BtnSquare%, BtnSquare
Hotkey, %JoyNum%Joy%BtnCross%, BtnCross
Hotkey, %JoyNum%Joy%BtnCircle%, BtnCircle
Hotkey, %JoyNum%Joy%BtnTriangle%, BtnTriangle
Hotkey, %JoyNum%Joy%BtnL1%, BtnL1
Hotkey, %JoyNum%Joy%BtnR1%, BtnR1

SetTimer, WatchJoy1, 10  ; Monitor the movement of the Left joystick.
SetTimer, WatchJoy2, 20  ; Monitor the movement of the Right joystick.

GetKeyState, JoyInfo, %JoyNum%JoyInfo
IfInString, JoyInfo, P  ; Joystick has POV control, so use it as a mouse wheel.
   SetTimer, MouseWheel, %WheelDelay%

return  ; End of auto-execute section.


; The subroutines below do not use KeyWait because that would sometimes trap the
; WatchJoystick quasi-thread beneath the wait-for-button-up thread, which would
; effectively prevent mouse-dragging with the joystick.

BtnSquare:
SetKeyDelay -1
Send, {%SquareKey% down}
SetTimer, WaitForBtnSquareUp, 10
return

BtnCross:
SetKeyDelay -1
Send, {%CrossKey% down}
SetTimer, WaitForBtnCrossUp, 10
return

BtnCircle:
SetKeyDelay -1
Send, {%CircleKey% down}
SetTimer, WaitForBtnCircleUp, 10
return

BtnTriangle:
SetKeyDelay -1
Send, {%TriangleKey% down}
SetTimer, WaitForBtnTriangleUp, 10
return

BtnL1:
SetMouseDelay, -1
MouseClick, left,,, 1, 0, D
SetTimer, WaitForBtnL1Up, 10
return

BtnR1:
SetMouseDelay, -1
MouseClick, right,,, 1, 0, D
SetTimer, WaitForBtnR1Up, 10
return


WaitForBtnSquareUp:
SetKeyDelay -1
GetKeyState, JoyStateSquare, %JoyNum%Joy%BtnSquare%
if JoyStateSquare = D  ; Button still down
   return
SetTimer, WaitForBtnSquareUp, off
Send, {%SquareKey% up}
return

WaitForBtnCrossUp:
SetKeyDelay -1
GetKeyState, JoyStateCross, %JoyNum%Joy%BtnCross%
if JoyStateCross = D  ; Button still down
   return
SetTimer, WaitForBtnCrossUp, off
Send, {%CrossKey% up}
return

WaitForBtnCircleUp:
SetKeyDelay -1
GetKeyState, JoyStateCircle, %JoyNum%Joy%BtnCircle%
if JoyStateCircle = D  ; Button still down
   return
SetTimer, WaitForBtnCircleUp, off
Send, {%CircleKey% up}
return

WaitForBtnTriangleUp:
SetKeyDelay -1
GetKeyState, JoyStateTriangle, %JoyNum%Joy%BtnTriangle%
if JoyStateTriangle = D  ; Button still down
   return
SetTimer, WaitForBtnTriangleUp, off
Send, {%TriangleKey% up}
return


WaitForBtnL1Up:
GetKeyState, JoyStateL1, %JoyNum%Joy%BtnL1%
if JoyStateL1 = D
   return
SetTimer, WaitForBtnL1Up, off
SetMouseDelay, -1
MouseClick, left,,, 1, 0, U
return

WaitForBtnR1Up:
GetKeyState, JoyStateR1, %JoystickNumber%Joy%BtnR1%
if JoyStateR1 = D
   return
SetTimer, WaitForBtnR1Up, off
MouseClick, right,,, 1, 0, U
return



WatchJoy1:
SetKeyDelay -1
SetFormat, float, 03
GetKeyState, joyx, %JoyNum%JoyX
GetKeyState, joyy, %JoyNum%JoyY
if joyx > %JoyUpper%
{
   Send, {%JoyRightKey% down}
}
else if joyx < %JoyLower%
{
   Send, {%JoyLeftKey% down}
}
else
{
   Send, {%JoyLeftKey% up}
   Send, {%JoyRightKey% up}
}

if joyy > %JoyUpper%
{
   Send, {%JoyDownKey% down}
}
else if joyy < %JoyLower%
{
   Send, {%JoyUpKey% down}
}
else
{
   Send, {%JoyUpKey% up}
   Send, {%JoyDownKey% up}
}


return


WatchJoy2:
MoveMouse? = n  ; Set default.
SetFormat, float, 03
GetKeyState, joyr, %JoyNum%JoyR
GetKeyState, joyz, %JoyNum%JoyZ
if joyr > %JoyUpper%
{
   MoveMouse? = y
   DeltaR = %joyr%
   DeltaR -= %JoyUpper%
}
else if joyr < %JoyLower%
{
   MoveMouse? = y
   DeltaR = %joyr%
   DeltaR -= %JoyLower%
}
else
   DeltaR = 0
if joyz > %JoyUpper%
{
   MoveMouse? = y
   DeltaZ = %joyz%
   DeltaZ -= %JoyUpper%
}
else if joyz < %JoyLower%
{
   MoveMouse? = y
   DeltaZ = %joyz%
   DeltaZ -= %JoyLower%
}
else
   DeltaZ = 0
if MoveMouse? = y
{
   DeltaR *= %JoyMultiplier%
   DeltaZ *= %JoyMultiplier%
   SetMouseDelay, -1  ; Makes movement smoother.
   MouseMove, %DeltaR%, %DeltaZ%, 0, R
}
return


MouseWheel:
GetKeyState, JoyPOV, %JoyNum%JoyPOV
if JoyPOV = -1  ; No angle.
   return
if (JoyPOV > 31500 or JoyPOV < 4500)  ; Forward
   Send {WheelUp}
else if JoyPOV between 13500 and 22500  ; Back
   Send {WheelDown}
return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 2nd, 2005, 10:53 pm 
Offline

Joined: November 29th, 2005, 7:07 am
Posts: 5
ok maybe that was too much info. can i start out with just a few questions then. is there a specific reason why my modifier keys wont work. are there specific reasons why when i set a joystick to a key the script will repeatedly keep pressing that key instead of press it once till the key is released


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2005, 12:08 am 
Offline

Joined: August 24th, 2005, 5:17 pm
Posts: 1237
"when i set a joystick to a key the script will repeatedly keep pressing that key instead of press it once till the key is released"

I've not scripted anything with joysticks, but I'm guessing the script is just doing what it's told - since the key is effectively held down, it keeps repeating or triggering the hotkey event.

Try either turning off the down hotkey until the hotkey is released (up) or using If to check whether the key has been released by setting a variable upon up/down.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2005, 12:11 am 
Offline

Joined: December 1st, 2005, 7:04 pm
Posts: 37
Location: Los Angeles, CA
too much code at once man - you'd get better answers (at least from me) if you posted a question with the fragment of the code where the problem was.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 3rd, 2005, 12:27 am 
Offline

Joined: November 29th, 2005, 7:07 am
Posts: 5
evl wrote:
Try either turning off the down hotkey until the hotkey is released (up) or using If to check whether the key has been released by setting a variable upon up/down.


Ah! Thanks that makes perfect sense. /me runs off to set a hotkey disable event.

Jesse wrote:
too much code at once man - you'd get better answers (at least from me) if you posted a question with the fragment of the code where the problem was.


Erm, I know. I didnt mean to. I noticed afterwards how it might be too much. As far as including the specific parts of the code, I really dont know where the error is. Basically i can use all of the example code and ill still be able to use the modifier keys. When i start with the joystick example code and add all of the joystick keys...None of my modifiers work any more.

EDIT: As an example in the above code compared to the Joystick-To-Mouse example. once i add the hotkeys for the joystick to map to the keyboard thats when the modifiers break.

EDIT2: I tried to set the hotkeys on and off and it still has the same error. So i then set a tooltip to tell me how many times i was calling the button label and to my suprise its only calling it ONCE per key press. That leads me to believe that sometimes maybe the program that im in requires someother verification as to whether the key is still down or not. i look up some windows api info and see what i come up with.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 4th, 2005, 1:19 pm 
Offline
User avatar

Joined: December 20th, 2004, 12:19 pm
Posts: 798
Location: LooseChange911.com Ask Questions, Demand Answers █ The WTC bldgs █ shouldn't have fallen █ that fast
NecriShiite wrote:
...the script will repeatedly keep pressing that key instead of press it once till the key is released

My answer to someone else: ...is there a way to make it go off only once per each press of the key?

...ok that was gonna be my answer, but upon glancing at the code...you specifically...

Code:
BtnSquare:
SetKeyDelay -1
Send, {%SquareKey% down}
SetTimer, WaitForBtnSquareUp, 10
return

...then...

Code:
WaitForBtnSquareUp:
SetKeyDelay -1
GetKeyState, JoyStateSquare, %JoyNum%Joy%BtnSquare%
if JoyStateSquare = D  ; Button still down
   return
SetTimer, WaitForBtnSquareUp, off
Send, {%SquareKey% up}
return

...you are making it hold down Enter...just use...(not tested)...

Code:
BtnSquare:
SetKeyDelay -1
Send, {%SquareKey%}
Gosub, WaitForBtnSquareUp
return

...and...

Code:
WaitForBtnSquareUp:
Loop
{
   SetKeyDelay -1
   GetKeyState, JoyStateSquare, %JoyNum%Joy%BtnSquare%
   if JoyStateSquare = U  ; Button up
      return
   Sleep, 19
}
return

...I'm hoping the Gosub, instead of a Timer will hold the execution of the hotkey until you release it & not keep firing it (which is different than the keyboard repeat rate {they produce the same effect of repeating keys tho}).

Version 2...just incase making the Timer a Gosub don't work very well...

Code:
BtnSquare:
Hotkey, %JoyNum%Joy%BtnSquare%, off
SetKeyDelay -1
Send, {%SquareKey%}
SetTimer, WaitForBtnSquareUp, 10
return

...and...

Code:
WaitForBtnSquareUp:
SetKeyDelay -1
GetKeyState, JoyStateSquare, %JoyNum%Joy%BtnSquare%
if JoyStateSquare = D  ; Button still down
   return
SetTimer, WaitForBtnSquareUp, off
Hotkey, %JoyNum%Joy%BtnSquare%, on
return

Version 0...getting back to my original idea of KeyWait (which I didn't think would work with Joystick keys, but now that I see the "Hotkey, %JoyNum%Joy%BtnSquare%" code, you can probably do this...

Code:
BtnSquare:
SetKeyDelay -1
Send, {%SquareKey%}
KeyWait, %JoyNum%Joy%BtnSquare%
return

...then...

...nothing...no Timer/Gosub this way.

Hrm...

Code:
; The subroutines below do not use KeyWait because that would sometimes trap the
; WatchJoystick quasi-thread beneath the wait-for-button-up thread, which would
; effectively prevent mouse-dragging with the joystick.

...damnit! Well if one of these don't work, then I don't know, I don't have a way to connect my PS2 to my comp to test (nor do I have WoW).

NecriShiite wrote:
is there a specific reason why my modifier keys wont work.

...what are your modifier keys? To me, modifier keys are Ctrl/Shift/Alt/Win...are you trying to make a PS2 key be a modifier key? Which ones? Ok, re-reading, I think you DO mean the normal modifier keys (you're not trying to create any on the PS2). I don't see anything that would force them not to work, but open the AHK window while the script is running & go to View -> Hotkeys & see if any of them are specified as a hotkey. If nothing else, you should be able to...

Code:
$*LCtrl::Send, {LCtrl down}
$*LCtrl up::Send, {LCtrl up}

...or on newer versions...

Code:
LCtrl::LCtrl

...or maybe...

Code:
$LCtrl::LCtrl

...to force AutoHotkey to control the modifiers & make them send themself.

_________________
AutoHotkey-Hotstring.ahk - Helping the world spell "AutoHotkey" correctly! (btw, it's a lowercase k!)


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Thanks!
PostPosted: December 10th, 2005, 4:54 am 
Offline

Joined: November 29th, 2005, 7:07 am
Posts: 5
JSLover wrote:
Code:
$*LCtrl::Send, {LCtrl down}
$*LCtrl up::Send, {LCtrl up}

...or on newer versions...

Code:
LCtrl::LCtrl

...or maybe...

Code:
$LCtrl::LCtrl

...to force AutoHotkey to control the modifiers & make them send themself.


This does wonders for the performance of the script. The modifier keys now work like a charm. As for the rest of the code i rewrote the entire thing to work in a loop to "Scan" the buttons every so often "100 times a second" and its working perfectly in WOW...


I will post the code when i get home tonight


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, Google Feedfetcher, Yahoo [Bot] and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group