[Windows] Control mouse cursor with a joystick
Posted: Tue Jul 12, 2011 3:05 pm
Hi all, I recently got a "Frisby el-cheapo gamepad" for my laptop. It works great, I used it to play Mortal Kombat 2 on a sega emulator, then got angry and annoyed because I kept losing... Anyway, I wondered what neat things I might beable to do with it, and the first thing that popped into my head was moving the mouse through a joy stick.
This code does not check if you have a joystick installed and automatically assumes your game controller is using JOYSTICKID1.
This code does not check if you have a joystick installed and automatically assumes your game controller is using JOYSTICKID1.
Code: Select all
Import "Winmm.lib"
joyGetPos(uJoyID.i, pji.l)As"_joyGetPos@8"
EndImport
;IncludeFile "WinAPI.pbi"
#JOYSTICK_DEFAULT_VALUE = 32767 ; values 32767 come from debugging pji\wXpos and pji\wYpos, change this to whatever your joystick defaults to
Structure jinfo : wXpos.i : wYpos.i : wZpos.i : wButtons.i : EndStructure
Structure cpoint : x.l : y.l : EndStructure
pji.jinfo
cp.cpoint
OpenWindow(#PB_Any, 0, 0, 200, 200, "Gamepad joystick thingy", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
Repeat : eventID = WaitWindowEvent(1)
Delay(1)
joyGetPos(#JOYSTICKID1,pji)
; -- Octo-directional joystick movement, too dumb to do any math :(
If pji\wXpos > #JOYSTICK_DEFAULT_VALUE : xBit = 1 : ElseIf pji\wXpos < #JOYSTICK_DEFAULT_VALUE : xBit = -1 : Else : xBit = 0 : EndIf
If pji\wYpos > #JOYSTICK_DEFAULT_VALUE : yBit = 1 : ElseIf pji\wYpos < #JOYSTICK_DEFAULT_VALUE : yBit = -1 : Else : yBit = 0 : EndIf
GetCursorPos_(cp)
SetCursorPos_(cp\x+xBit, cp\y+yBit)
Until eventID = #PB_Event_CloseWindow