[Windows] Control mouse cursor with a joystick

Share your advanced PureBasic knowledge/code with the community.
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

[Windows] Control mouse cursor with a joystick

Post by Nituvious »

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.

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
▓▓▓▓▓▒▒▒▒▒░░░░░
zefiro_flashparty
User
User
Posts: 74
Joined: Fri Mar 04, 2005 7:46 pm
Location: argentina

Re: [Windows] Control mouse cursor with a joystick

Post by zefiro_flashparty »

^^ XD nice, i test bur the mouse walk alone :p ned a dead zone, to ignore non-calibrated pulses
Amd Vishera fx8350 ,16Gbram, Gtx650 ti, 2gb,Win 10pro. 13tbs. 8)
Post Reply