AutoHotkey Community

It is currently May 27th, 2012, 4:27 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: February 7th, 2010, 1:29 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
I'm still working on a larger tweaking/enhancement script for Win7, but I decided to release a small part of code I wrote up yesterday.
The idea is to use a gamepad (in my case, an xbox360 controller) as a remote control, for watching movies from your bed, checking a webpage, etc ;)
Video watching is targeted at VLC, but it might work well with others too. You can play/pause with the start button, and adjust volume with the second analog stick.
I assigned mouse (cursor, buttons and wheel) to the gamepad, arrow keys, space, enter and alt+up (upwards in explorer) to buttons, also I added the zoom function so you can read text more easily from distance. I also applied some simple math for better cursor control.

Code:
sign(x)
{
   if(x!=0)
      return x/abs(x)
   else
      return 1
}


SetTimer, CheckJoystick, 5
SetTimer, Arrows, 70

;Run this function at startup if you include this file in other scripts
JoystickStart()
{
   SetTimer, CheckJoystick, 5
   SetTimer, Arrows, 70
}
CheckJoystick:
XAxis:=0
YAxis:=0
GetKeyState, XAxis, JoyX ;Get axis data
GetKeyState, YAxis, JoyY
GetKeyState, RAxis, JoyR
GetKeyState, UAxis, JoyU

if(RAxis<20) ;Mouse wheel
   Send {WheelUp}
if(RAxis>80)
   Send {WheelDown}
if(UAxis<20)
   Send {WheelLeft}
if(UAxis>80)
   Send {WheelRight}
if(XAxis||YAxis)
{
   XAxis-=50 ;Mouse cursor
   YAxis-=50
   if(abs(XAxis)<10)
      XAxis:=10*sign(XAxis)
   if(abs(YAxis)<10)
      YAxis:=10*sign(YAxis)
   XAxis-=10*sign(XAxis)
   YAxis-=10*sign(YAxis)
   XAxis:=0.1*abs(Round(XAxis))**1.5*sign(XAxis)
   YAxis:=0.1*abs(Round(YAxis))**1.5*sign(YAxis)
   MouseMove, XAxis,YAxis,0,R
}
return

Arrows: ;Arrows have separate label because of timing
GetKeyState, POV, JoyPOV
if(POV>=0)
{
   if(POV=0||POV=4500||POV=31500)
      Send {Up}
   if(POV=22500||POV=27000||POV=31500)
      Send {Left}
   if(POV=13500||POV=18000||POV=22500)
      Send {Down}
   if(POV=4500||POV=9000||POV=13500)
      Send {Right}
}
return

Joy1::
SetMouseDelay, -1  ; Makes movement smoother.
MouseClick, left,,, 1, 0, D  ; Hold down the left mouse button.
SetTimer, WaitForLeftButtonUp, 10
return

Joy2::
SetMouseDelay, -1  ; Makes movement smoother.
MouseClick, right,,, 1, 0, D  ; Hold down the right mouse button.
SetTimer, WaitForRightButtonUp, 10
return

Joy3::
SetMouseDelay, -1  ; Makes movement smoother.
MouseClick, middle,,, 1, 0, D  ; Hold down the right mouse button.
SetTimer, WaitForMiddleButtonUp, 10
return

WaitForLeftButtonUp:
if GetKeyState("Joy1")
    return  ; The button is still, down, so keep waiting.
; Otherwise, the button has been released.
SetTimer, WaitForLeftButtonUp, off
SetMouseDelay, -1  ; Makes movement smoother.
MouseClick, left,,, 1, 0, U  ; Release the mouse button.
return

WaitForRightButtonUp:
if GetKeyState("Joy2")
    return  ; The button is still, down, so keep waiting.
; Otherwise, the button has been released.
SetTimer, WaitForRightButtonUp, off
SetMouseDelay, -1  ; Makes movement smoother.
MouseClick, right,,, 1, 0, U  ; Release the mouse button.
return

WaitForMiddleButtonUp:
if GetKeyState("Joy3")
    return  ; The button is still, down, so keep waiting.
; Otherwise, the button has been released.
SetTimer, WaitForMiddleButtonUp, off
SetMouseDelay, -1  ; Makes movement smoother.
MouseClick, middle,,, 1, 0, U  ; Release the mouse button.
return

Joy4::Send {Return}
Joy5::Send #{NumpadSub}
Joy6::Send #{NumpadAdd}
Joy7::Send !{Up}
Joy8::Send {Space}


Feel free to tweak it to your needs, and let me know of possible improvements. I haven't found a use for the "trigger" axis buttons of the xbox controller yet, I could probably add the on-screen keyboard if you need to type something. Also the Mouse wheel left/right doesn't appear to work for me, maybe because my mouse doesn't have it? Just a wild guess, I'm running latest ahk_l.


Last edited by fragman on February 25th, 2010, 11:17 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2010, 3:24 pm 
WoW Ty dude! You make my day with that script! I was looking for smthing like that for games espesally AION now :)

HF


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2010, 3:28 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
I'm probably going to add some fullscreen detection to exclude games, but you can use this version or disable it then.
I will probably "re-include" a few apps like media players, fullscreen browsers,... , so that hotkeys etc work there.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2010, 10:01 pm 
Offline

Joined: February 22nd, 2009, 7:24 pm
Posts: 21
Location: Dallas TX
Very nice script fragman, the abs axis evaluator makes it very clean. I was wondering since you use an xbox controller on win7 if you have seen any Xinput drivers for it. I wrote a scipt simmilar to this for XP but it was reliant on wrapping Xinput funtions to split the Z axis or triggers becuase they read as one axis with %50 at idle. So I had to rewrite it using postmessage to play WoW with it. Just wanted to add a sugestion that I use in my script, I use a seperate timer to evaluate the Z axis, and then use the result in other send parts of it to use the triggers as a kind of shift key. Just gives you allot more options if you start running out of buttons. Anyway, great script, thanks allot. :D

_________________
"lol, i made this thing, but it didn't work... so I read the forums and now it does!"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2010, 10:08 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
I haven't yet needed modifier keys, but I'll consider them when I require more keys. I consider this script mainly for remote controlling windows, not for use in games, but it may be adjusted as needed of course.

I couldn't get the XBCD driver working with my controller sadly, I would've liked to use the extended functionality.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 21st, 2010, 12:36 pm 
Offline

Joined: December 9th, 2009, 12:28 am
Posts: 9
seems great so far, although i cant use shift on my keyboard now for some reason. anyhow, exactly what i looked for. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 21st, 2010, 1:10 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
Are you sure this is related to this script?
I'm not doing anything with the shift key.
Also, is there a proper way to detect if the joystick/gamepad is connected?
I need this disabled if there is none.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2010, 1:47 pm 
Offline

Joined: December 9th, 2009, 12:28 am
Posts: 9
fragman wrote:
Are you sure this is related to this script?
I'm not doing anything with the shift key.
Also, is there a proper way to detect if the joystick/gamepad is connected?
I need this disabled if there is none.


Might be someting else, I have two othe autohotkey scripts running at the same time, however I've never experienced this side effect before. Maybe its a combination. And.., I dont know about checking if its avail.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2010, 11:19 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
I have updated the script so it doesn't screw up mouse movement when no joystick is connected.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 4 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