Need some help with events / rawinput mouse code!

Archives Forums/Win32 Discussion/Need some help with events / rawinput mouse code!

sswift(Posted 2009) [#1]
This is what I've got so far:

' hWnd needs to be set before initializing the event handler. 
' You can get this value with hWnd = GetActiveWindow(), or with hWnd = QueryGadget(Window, QUERY_HWND), where Window is your window's gadget pointer. (May require MaxGUI)


Global hWnd:Int 
Global OldWinProc:Int


OldWinProc = SetWindowLongPtr(hWnd, GWL_WNDPROC, WinProc)  


Function WinProc%(hWnd:Int, Msg:Int, wParam:Int, lParam:Int) "win32"
		
	Select Msg
	
		Case WM_RESIZE
			DebugLog("WM_RESIZE")

		Case WM_MOVE
			DebugLog("WM_MOVE")

		Case WM_CLOSE
			DebugLog("WM_CLOSE")

		Case WM_DESTROY
			DebugLog("WM_DESTROY")
			
			' Restore old winproc.
			SetWindowLong(hwnd, GWL_WNDPROC, OldWinProc) 

		Case WM_KEYDOWN
			DebugLog("WM_KEYDOWN")
			DebugLog("You pressed: " + Chr$(wParam))
	
		Case WM_CREATE
			DebugLog("WM_CREATE")	
	
		Case WM_INITDIALOG
			DebugLog("WM_INITDIALOG")	
		
		Case WM_COMMAND
			DebugLog("WM_COMMAND " + lParam)

	    Case WM_INPUT

	    		dwSize:Int = 40
			lpb:Byte[40]
    
			GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, SizeOf(RAWINPUTHEADER));
    
			RAWINPUT* raw = (RAWINPUT*)lpb;
    
			If (raw->header.dwType == RIM_TYPEMOUSE) 
				Int xPosRelative = raw->data.mouse.lLastX;
				Int yPosRelative = raw->data.mouse.lLastY;
			EndIf
				
		Default
			'DebugLog Msg
					
	End Select
	
	If OldWinProc <> 0 Then Return CallWindowProc(OldWinProc, hWnd, Msg, wParam, lParam)   
	
End Function


Function LOWORD(value)
	Return value & $FFFF
End Function

Function HIWORD(value)
	Return (value Shr 16)
End Function





Type RAWINPUTDEVICE 
   Field usUsagePage:Short
   Field usUsage:Short
   Field dwFlags:Int
   Field hwndTarget:Int
End Type

Const HID_USAGE_PAGE_GENERIC  = $1
Const HID_USAGE_GENERIC_MOUSE = $2
Const RIDEV_INPUTSINK = $100

Global Rid:RAWINPUTDEVICE[1]

    Rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC
    Rid[0].usUsage = HID_USAGE_GENERIC_MOUSE 
    Rid[0].dwFlags = RIDEV_INPUTSINK   
    Rid[0].hwndTarget = hWnd					' hWnd needs to be set by the time we get here, so this should be in an init function.

    RegisterRawInputDevices(Rid, 1, SizeOf(Rid[0])



Extern "Win32"

	Function RegisterRawInputDevices:Int(pRawInputDevices:Byte Ptr, uiNumDevices:Int, cbSize:Int)
	Function GetRawInputData:Int(HRAWINPUT hRawInput, uiCommand:Int, LPVOID pData, PUINT pcbSize, cbSizeHeader:Int)
	
End Extern



Where I'm currently stuck is declaring that second extern at the bottomn. Then I need to figure out what that C code in the WM_INPUT event is doing...


sswift(Posted 2009) [#2]
It would seem that HRAWINPUT type is a handle, which is a void pointer, which is a pointer to any type. I have no idea how to declare such a thing in BlitzMax though.


markcw(Posted 2009) [#3]
Object and void pointers in C are all byte pointers in Max, if it's a variable pointer you use the corresponding data type, so this should be right.
Function GetRawInputData:Int(hRawInput:Byte Ptr, uiCommand:Int, pData:Byte Ptr, pcbSize:Int Ptr, cbSizeHeader:Int)