Can raw keyboard input be implemented????

Can raw keyboard input be implemented????

Postby Mike Buckler » Mon Sep 22, 2008 11:17 pm

Can raw keyboard input be implemented?

The Idea is-
You have a multi monitor pc.
1st execution of your application is displayed on monitor 1.
2nd execution of your application is displayed on monitor 2.
3rd execution of your application is displayed on monitor 3.
4th execution of your application is displayed on monitor 4.
5th execution of your application is displayed on monitor 5.
6th execution of your application is displayed on monitor 6.

Then each application executing could be assigned a keyboard to use.
(Based on the keyboard device handle.)

You then have a 6 terminal application with only 1 pc and no network.
This would create big money savings for your end users.

I found an article on The Code Project that shows how to do this.
Link http://www.codeproject.com/KB/system/rawinput.aspx

You can download an install of the project from http://trimpos.net/RawInput.zip
And install multiple keyboards on one pc and see the keyboard device handle change each time you press a key on the different keyboards.

I have no idea where or how to implement in FiveWin / Xharbour applications any help would be appreciated.

Mike.


[url][/url]
Mike Buckler
 
Posts: 67
Joined: Thu Jan 05, 2006 10:35 pm
Location: Canada

Postby Antonio Linares » Tue Sep 23, 2008 9:41 pm

Mike,

It doesn't look difficult to be implemented:
http://msdn.microsoft.com/en-us/library/ms645590(VS.85).aspx

As a start you could inherit a new Class TMyWindow FROM TWindow and redefine the Method HandleEvent() to process WM_INPUT messages, once the application has be registered to receive raw input messages, using RegisterRawInputDevices().

Just a curiosity: If several users use the same computer, how will you control them from doing some tasks that may generate a conflict ? Or will you just allow them to use your application ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Can anyone help with the dll32 function calls.

Postby Mike Buckler » Wed Sep 24, 2008 10:42 pm

Antonio – I am hoping that when I implement this into an executable that it would only be active in that executable.

Can anyone help with the dll32 function calls to user32.dll to process the raw keyboard calls. I have no idea where to start with this. Once I can see how to get the keyboards handle and the key pressed I will be off and running. If I can get this working I will post an example so that anyone can use.
I found an article on The Code Project that shows how to do this.
Link http://www.codeproject.com/KB/system/rawinput.aspx

Any help would be greatly appreciated.
Thanks in advance.
Mike
Mike Buckler
 
Posts: 67
Joined: Thu Jan 05, 2006 10:35 pm
Location: Canada

Postby Antonio Linares » Thu Sep 25, 2008 11:21 am

Mike,

Here you have a first prototype. Actually it is just receiving the keyboard events. We need to find the flag for the mouse events too.

Click on the window and then press some keyboard keys. You will hear "beeps" :-)

Though this example is already workable, please notice that this is a work in progress:

rawinput.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

#define WM_INPUT  0x00FF

function Main()

   local oWnd := TMyWindow():New()

   oWnd:SetText( "Raw Input test" )
   
   MsgInfo( RegisterRawInputDevices( oWnd:hWnd ) )

   oWnd:Activate()

return nil

CLASS TMyWindow FROM TWindow

   CLASSDATA lRegistered

   METHOD HandleEvent( nMsg, nWParam, nLParam )

ENDCLASS

METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TMyWindow

   if nMsg == WM_INPUT
      MsgBeep() // ok, we are receiving WM_INPUT messages
   endif   

return Super:HandleEvent( nMsg, nWParam, nLParam )

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

#define RIDEV_INPUTSINK 0x00000100

typedef struct tagRAWINPUTDEVICE
{
   USHORT usUsagePage;
   USHORT usUsage;
   DWORD dwFlags;
   HWND hwndTarget;
} RAWINPUTDEVICE, * PRAWINPUTDEVICE, * LPRAWINPUTDEVICE;

typedef BOOL WINAPI ( * PREGISTERRAWINPUTDEVICES ) (     
   PRAWINPUTDEVICE pRawInputDevices,
   UINT uiNumDevices,
   UINT cbSize
);

HB_FUNC( REGISTERRAWINPUTDEVICES )
{
   RAWINPUTDEVICE rid;
   PREGISTERRAWINPUTDEVICES pRegisterRawInputDevices;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   
   pRegisterRawInputDevices = ( PREGISTERRAWINPUTDEVICES ) GetProcAddress( hUser32, "RegisterRawInputDevices" );
   
   rid.usUsagePage = 0x01;
   rid.usUsage     = 0x06;
   rid.dwFlags     = RIDEV_INPUTSINK;
   rid.hwndTarget  = ( HWND ) hb_parnl( 1 );
   
   hb_retl( pRegisterRawInputDevices( &rid, 1, sizeof( RAWINPUTDEVICE ) ) );
         
   FreeLibrary( hUser32 );
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Thu Sep 25, 2008 11:38 am

This new version manages the mouse and the keyboard. Please notice that it will beep like crazy because it detects all the mouse movements :-)

Next we need to select the events that are just for our window:

rawinput.prg
Code: Select all  Expand view  RUN
#include "FiveWin.ch"

#define WM_INPUT  0x00FF

function Main()

   local oWnd := TMyWindow():New()

   oWnd:SetText( "Raw Input test" )
   
   MsgInfo( RegisterRawInputDevices( oWnd:hWnd ) )

   oWnd:Activate()

return nil

CLASS TMyWindow FROM TWindow

   CLASSDATA lRegistered

   METHOD HandleEvent( nMsg, nWParam, nLParam )

ENDCLASS

METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TMyWindow

   if nMsg == WM_INPUT
      MsgBeep() // ok, we are receiving WM_INPUT messages
   endif   

return Super:HandleEvent( nMsg, nWParam, nLParam )

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

#define RIDEV_INPUTSINK 0x00000100

typedef struct tagRAWINPUTDEVICE
{
   USHORT usUsagePage;
   USHORT usUsage;
   DWORD dwFlags;
   HWND hwndTarget;
} RAWINPUTDEVICE, * PRAWINPUTDEVICE, * LPRAWINPUTDEVICE;

typedef BOOL WINAPI ( * PREGISTERRAWINPUTDEVICES ) (     
   PRAWINPUTDEVICE pRawInputDevices,
   UINT uiNumDevices,
   UINT cbSize
);

HB_FUNC( REGISTERRAWINPUTDEVICES )
{
   RAWINPUTDEVICE rid[ 2 ];
   PREGISTERRAWINPUTDEVICES pRegisterRawInputDevices;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   
   pRegisterRawInputDevices = ( PREGISTERRAWINPUTDEVICES ) GetProcAddress( hUser32, "RegisterRawInputDevices" );
   
   rid[ 0 ].usUsagePage = 0x01;
   rid[ 0 ].usUsage     = 0x02; // Mouse
   rid[ 0 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 0 ].hwndTarget  = ( HWND ) hb_parnl( 1 );

   rid[ 1 ].usUsagePage = 0x01;
   rid[ 1 ].usUsage     = 0x06; // Keyboard
   rid[ 1 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 1 ].hwndTarget  = ( HWND ) hb_parnl( 1 );
   
   hb_retl( pRegisterRawInputDevices( rid, 2, sizeof( RAWINPUTDEVICE ) ) );
         
   FreeLibrary( hUser32 );
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Wow that’s great I can’t thank you enough.

Postby Mike Buckler » Thu Sep 25, 2008 11:47 pm

Wow that’s great I can’t thank you enough.
I was out of the office today and was very excited to see a response from you.
I don’t know if I understand correctly how this will work.
Will it be that we filter the device id for a particular keyboard and mouse for the active application?(one on each screen)
I have already put setups in my application to define the monitor to be displayed on up to 6 monitors.
Anyway I feel that this would be such a great contribution to FiveWin that I would like to offer to pay you for your time working on this, please bill me for your time.
Thank You Very Very Much
Mike S. Buckler
Mike Buckler
 
Posts: 67
Joined: Thu Jan 05, 2006 10:35 pm
Location: Canada

Postby Antonio Linares » Fri Sep 26, 2008 10:50 pm

A new version implementing GETRAWINPUTDATA():
Code: Select all  Expand view  RUN
// Windows Raw Input API support demo, (c) FiveTech Software

#include "FiveWin.ch"

#define WM_INPUT  0x00FF

function Main()

   local oWnd := TMyWindow():New()

   oWnd:SetText( "Raw Input test" )
   
   MsgInfo( RegisterRawInputDevices( oWnd:hWnd ) )

   oWnd:Activate()

return nil

CLASS TMyWindow FROM TWindow

   CLASSDATA lRegistered

   METHOD HandleEvent( nMsg, nWParam, nLParam )

ENDCLASS

METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TMyWindow

   if nMsg == WM_INPUT
      MsgBeep() // ok, we are receiving WM_INPUT messages
   endif   

return Super:HandleEvent( nMsg, nWParam, nLParam )

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

#define RIDEV_INPUTSINK 0x00000100

#define RID_INPUT       0x10000003

typedef struct tagRAWINPUTDEVICE
{
   USHORT usUsagePage;
   USHORT usUsage;
   DWORD dwFlags;
   HWND hwndTarget;
} RAWINPUTDEVICE, * PRAWINPUTDEVICE, * LPRAWINPUTDEVICE;

typedef BOOL WINAPI ( * PREGISTERRAWINPUTDEVICES ) (     
   PRAWINPUTDEVICE pRawInputDevices,
   UINT uiNumDevices,
   UINT cbSize
);

typedef struct tagRAWINPUTHEADER {
    DWORD dwType;
    DWORD dwSize;
    HANDLE hDevice;
    WPARAM wParam;
} RAWINPUTHEADER, * PRAWINPUTHEADER;

typedef struct tagRAWMOUSE {
  USHORT    usFlags;
  union {
         ULONG    ulButtons;
             struct {
                       USHORT usButtonFlags;
                       USHORT usButtonData;
                       };
  };
  ULONG ulRawButtons;
  LONG  lLastX;
  LONG  lLastY;
  ULONG ulExtraInformation;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;

typedef struct tagRAWKEYBOARD {
    USHORT MakeCode;
    USHORT Flags;
    USHORT Reserved;
    USHORT VKey;
    UINT Message;
    ULONG ExtraInformation;
} RAWKEYBOARD, *PRAWKEYBOARD, *LPRAWKEYBOARD;

typedef struct tagRAWHID {
    DWORD dwSizeHid;
    DWORD dwCount;
    BYTE bRawData;
} RAWHID, **LPRAWHID;

typedef struct tagRAWINPUT {
    RAWINPUTHEADER header;
    union {
       RAWMOUSE    mouse;
       RAWKEYBOARD keyboard;
       RAWHID      hid;
    } data;
} RAWINPUT, * PRAWINPUT, * LPRAWINPUT;

typedef UINT WINAPI ( * PGETRAWINPUTDATA ) (     
    LPRAWINPUT hRawInput,
    UINT uiCommand,
    LPVOID pData,
    PUINT pcbSize,
    UINT cbSizeHeader
);

HB_FUNC( REGISTERRAWINPUTDEVICES )
{
   RAWINPUTDEVICE rid[ 2 ];
   PREGISTERRAWINPUTDEVICES pRegisterRawInputDevices;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   
   pRegisterRawInputDevices = ( PREGISTERRAWINPUTDEVICES ) GetProcAddress( hUser32, "RegisterRawInputDevices" );
   
   rid[ 0 ].usUsagePage = 0x01;
   rid[ 0 ].usUsage     = 0x02; // Mouse
   rid[ 0 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 0 ].hwndTarget  = ( HWND ) hb_parnl( 1 );

   rid[ 1 ].usUsagePage = 0x01;
   rid[ 1 ].usUsage     = 0x06; // Keyboard
   rid[ 1 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 1 ].hwndTarget  = ( HWND ) hb_parnl( 1 );
   
   hb_retl( pRegisterRawInputDevices( rid, 2, sizeof( RAWINPUTDEVICE ) ) );
         
   FreeLibrary( hUser32 );
}

HB_FUNC( GETRAWINPUTDATA )
{
   UINT dwSize;
   LPBYTE lpb;
   PGETRAWINPUTDATA pGetRawInputData;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );

   pGetRawInputData = ( PGETRAWINPUTDATA ) GetProcAddress( hUser32, "GetRawInputData" );
   
   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_INPUT, NULL, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
                     
   lpb = ( LPBYTE ) hb_xgrab( dwSize );                 

   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_INPUT, lpb, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
   hb_retnl( ( unsigned long ) lpb );                 
                     
   FreeLibrary( hUser32 );
}                   

#pragma ENDDUMP

Next we are going to list the available HIDs (Human Interface Devices) to select the ones that we want to use from our applications.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Mike Buckler » Fri Sep 26, 2008 11:12 pm

Thanks for your time Antonio
Mike.
Mike Buckler
 
Posts: 67
Joined: Thu Jan 05, 2006 10:35 pm
Location: Canada

Postby Antonio Linares » Fri Sep 26, 2008 11:16 pm

Mike,

Lets see if we get a good and well designed Raw Input implementation, as simple as possible :-)

I am still reading a lot about this interesting Windows API to find the right way to do it and make it real easy to be used :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Sat Sep 27, 2008 12:12 am

This version already list the available HIDs.

Mike, please connect several keyboards and mouses and lets see how they look like in the list, thanks :-)
Code: Select all  Expand view  RUN
// Windows Raw Input API support demo, (c) FiveTech Software

#include "FiveWin.ch"

#define WM_INPUT  0x00FF

function Main()

   local aHIDevices := GetRawInputDeviceList()
   
   MsgSelect( aHIDevices, "", "Human Interface Devices" )
   
   /*
   local oWnd := TMyWindow():New()

   oWnd:SetText( "Raw Input test" )
   
   MsgInfo( RegisterRawInputDevices( oWnd:hWnd ) )

   oWnd:Activate()
   */

return nil

function MsgSelect( aItems, cValue, cTitle )

   local oDlg

   DEFAULT cTitle := "Please, select"

   DEFINE DIALOG oDlg FROM 5,10 TO 24, 80 TITLE cTitle

   @ 0.5, 0.7 LISTBOX cValue ITEMS aItems SIZE 268, 110 OF oDlg

   @ 7, 14 BUTTON "&OK" OF oDlg SIZE 40, 12 ;
     ACTION oDlg:End() DEFAULT

   @ 7, 24 BUTTON "&Cancel" OF oDlg SIZE 40, 12 ;
     ACTION ( cValue := nil, oDlg:End() )

   ACTIVATE DIALOG oDlg CENTERED

return cValue

CLASS TMyWindow FROM TWindow

   CLASSDATA lRegistered

   METHOD HandleEvent( nMsg, nWParam, nLParam )

ENDCLASS

METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TMyWindow

   if nMsg == WM_INPUT
      MsgBeep() // ok, we are receiving WM_INPUT messages
   endif   

return Super:HandleEvent( nMsg, nWParam, nLParam )

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

#define RIDEV_INPUTSINK 0x00000100
#define RID_INPUT       0x10000003
#define RIDI_DEVICENAME 0x20000007

typedef struct tagRAWINPUTDEVICE
{
   USHORT usUsagePage;
   USHORT usUsage;
   DWORD dwFlags;
   HWND hwndTarget;
} RAWINPUTDEVICE, * PRAWINPUTDEVICE, * LPRAWINPUTDEVICE;

typedef BOOL WINAPI ( * PREGISTERRAWINPUTDEVICES ) (     
   PRAWINPUTDEVICE pRawInputDevices,
   UINT uiNumDevices,
   UINT cbSize
);

typedef struct tagRAWINPUTHEADER {
    DWORD dwType;
    DWORD dwSize;
    HANDLE hDevice;
    WPARAM wParam;
} RAWINPUTHEADER, * PRAWINPUTHEADER;

typedef struct tagRAWMOUSE {
  USHORT    usFlags;
  union {
         ULONG    ulButtons;
             struct {
                       USHORT usButtonFlags;
                       USHORT usButtonData;
                       };
  };
  ULONG ulRawButtons;
  LONG  lLastX;
  LONG  lLastY;
  ULONG ulExtraInformation;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;

typedef struct tagRAWKEYBOARD {
    USHORT MakeCode;
    USHORT Flags;
    USHORT Reserved;
    USHORT VKey;
    UINT Message;
    ULONG ExtraInformation;
} RAWKEYBOARD, *PRAWKEYBOARD, *LPRAWKEYBOARD;

typedef struct tagRAWHID {
    DWORD dwSizeHid;
    DWORD dwCount;
    BYTE bRawData;
} RAWHID, **LPRAWHID;

typedef struct tagRAWINPUT {
    RAWINPUTHEADER header;
    union {
       RAWMOUSE    mouse;
       RAWKEYBOARD keyboard;
       RAWHID      hid;
    } data;
} RAWINPUT, * PRAWINPUT, * LPRAWINPUT;

typedef UINT WINAPI ( * PGETRAWINPUTDATA ) (     
    LPRAWINPUT hRawInput,
    UINT uiCommand,
    LPVOID pData,
    PUINT pcbSize,
    UINT cbSizeHeader
);

typedef struct tagRAWINPUTDEVICELIST {
    HANDLE hDevice;
    DWORD dwType;
} RAWINPUTDEVICELIST, * PRAWINPUTDEVICELIST;

typedef UINT WINAPI ( * PGETRAWINPUTDEVICELIST ) (     
    PRAWINPUTDEVICELIST pRawInputDeviceList,
    PUINT puiNumDevices,
    UINT cbSize
);

typedef UINT WINAPI ( * PGETRAWINPUTDEVICEINFO ) (     
    HANDLE hDevice,
    UINT uiCommand,
    LPVOID pData,
    PUINT pcbSize
);

HB_FUNC( REGISTERRAWINPUTDEVICES )
{
   RAWINPUTDEVICE rid[ 2 ];
   PREGISTERRAWINPUTDEVICES pRegisterRawInputDevices;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   
   pRegisterRawInputDevices = ( PREGISTERRAWINPUTDEVICES ) GetProcAddress( hUser32, "RegisterRawInputDevices" );
   
   rid[ 0 ].usUsagePage = 0x01;
   rid[ 0 ].usUsage     = 0x02; // Mouse
   rid[ 0 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 0 ].hwndTarget  = ( HWND ) hb_parnl( 1 );

   rid[ 1 ].usUsagePage = 0x01;
   rid[ 1 ].usUsage     = 0x06; // Keyboard
   rid[ 1 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 1 ].hwndTarget  = ( HWND ) hb_parnl( 1 );
   
   hb_retl( pRegisterRawInputDevices( rid, 2, sizeof( RAWINPUTDEVICE ) ) );
         
   FreeLibrary( hUser32 );
}

HB_FUNC( GETRAWINPUTDATA )
{
   UINT dwSize;
   LPBYTE lpb;
   PGETRAWINPUTDATA pGetRawInputData;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );

   pGetRawInputData = ( PGETRAWINPUTDATA ) GetProcAddress( hUser32, "GetRawInputData" );
   
   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_INPUT, NULL, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
                     
   lpb = ( LPBYTE ) hb_xgrab( dwSize );                 

   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_INPUT, lpb, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
   hb_retnl( ( unsigned long ) lpb );                 
                     
   FreeLibrary( hUser32 );
}                   

HB_FUNC( GETRAWINPUTDEVICELIST )
{
   UINT nDevices;
   PRAWINPUTDEVICELIST pRawInputDeviceList;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   PGETRAWINPUTDEVICELIST pGetRawInputDeviceList;
   PGETRAWINPUTDEVICEINFO pGetRawInputDeviceInfo;

   pGetRawInputDeviceList = ( PGETRAWINPUTDEVICELIST ) GetProcAddress( hUser32, "GetRawInputDeviceList" );
   pGetRawInputDeviceInfo = ( PGETRAWINPUTDEVICEINFO ) GetProcAddress( hUser32, "GetRawInputDeviceInfoA" );
   
   pGetRawInputDeviceList( NULL, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
   
   if( nDevices )
   {
      UINT ui;
     
      pRawInputDeviceList = ( PRAWINPUTDEVICELIST ) hb_xgrab( sizeof( RAWINPUTDEVICELIST ) * nDevices );
     
      pGetRawInputDeviceList( pRawInputDeviceList, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
     
      hb_reta( nDevices );
     
      for( ui = 0; ui < nDevices; ui++ )
      {
         char name[ 512 ];
         UINT uiSize = 511;
         
         pGetRawInputDeviceInfo( pRawInputDeviceList[ ui ].hDevice, RIDI_DEVICENAME, ( void * ) name, &uiSize );
         hb_storclen( name, uiSize, -1, ui + 1 );
      }
     
      hb_xfree( ( void * ) pRawInputDeviceList );
   }   

   FreeLibrary( hUser32 );
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Sat Sep 27, 2008 12:16 am

Keyboard and Mouse devices seem to use KBD and MOU in their names:
Image
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Sat Sep 27, 2008 12:19 am

Here you have the EXE and PRG for those of you that want to try it on your computers:
http://rapidshare.com/files/148687954/rawinput.zip.html
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Sat Sep 27, 2008 12:29 am

I think that the easiest implementation would be something like this :-)

SET KEYBOARD TO 1
SET MOUSE TO 1

For another application:

SET KEYBOARD TO 2
SET MOUSE TO 2

or even simpler: The application automatically selects the next available device (?)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Sat Sep 27, 2008 7:51 am

A new version that counts the number of mouses and returns the handle for each one.
Code: Select all  Expand view  RUN
// Windows Raw Input API support demo, (c) FiveTech Software

#include "FiveWin.ch"

#define WM_INPUT  0x00FF

function Main()

   local nMouses := GetRawMouses(), n

   MsgInfo( "Number of available mouses: " + AllTrim( Str( nMouses ) ) )
   
   for n = 1 to nMouses
      MsgInfo( GetRawMouse( n ), "Mouse " + AllTrim( Str( n ) ) )
   next   
   
   /*
   local oWnd := TMyWindow():New()

   oWnd:SetText( "Raw Input test" )
   
   MsgInfo( RegisterRawInputDevices( oWnd:hWnd ) )

   oWnd:Activate()
   */

return nil

function MsgSelect( aItems, cValue, cTitle )

   local oDlg

   DEFAULT cTitle := "Please, select"

   DEFINE DIALOG oDlg FROM 5,10 TO 24, 80 TITLE cTitle

   @ 0.5, 0.7 LISTBOX cValue ITEMS aItems SIZE 268, 110 OF oDlg

   @ 7, 14 BUTTON "&OK" OF oDlg SIZE 40, 12 ;
     ACTION oDlg:End() DEFAULT

   @ 7, 24 BUTTON "&Cancel" OF oDlg SIZE 40, 12 ;
     ACTION ( cValue := nil, oDlg:End() )

   ACTIVATE DIALOG oDlg CENTERED

return cValue

CLASS TMyWindow FROM TWindow

   CLASSDATA lRegistered

   METHOD HandleEvent( nMsg, nWParam, nLParam )

ENDCLASS

METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TMyWindow

   if nMsg == WM_INPUT
      MsgBeep() // ok, we are receiving WM_INPUT messages
   endif   

return Super:HandleEvent( nMsg, nWParam, nLParam )

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

#define RIDEV_INPUTSINK 0x00000100
#define RID_INPUT       0x10000003
#define RIDI_DEVICENAME 0x20000007
#define RIDI_DEVICEINFO 0x2000000b

#define RIM_TYPEMOUSE       0
#define RIM_TYPEKEYBOARD    1
#define RIM_TYPEHID         2

typedef struct tagRAWINPUTDEVICE
{
   USHORT usUsagePage;
   USHORT usUsage;
   DWORD dwFlags;
   HWND hwndTarget;
} RAWINPUTDEVICE, * PRAWINPUTDEVICE, * LPRAWINPUTDEVICE;

typedef BOOL WINAPI ( * PREGISTERRAWINPUTDEVICES ) (     
   PRAWINPUTDEVICE pRawInputDevices,
   UINT uiNumDevices,
   UINT cbSize
);

typedef struct tagRAWINPUTHEADER {
    DWORD dwType;
    DWORD dwSize;
    HANDLE hDevice;
    WPARAM wParam;
} RAWINPUTHEADER, * PRAWINPUTHEADER;

typedef struct tagRAWMOUSE {
  USHORT    usFlags;
  union {
         ULONG    ulButtons;
             struct {
                       USHORT usButtonFlags;
                       USHORT usButtonData;
                       };
  };
  ULONG ulRawButtons;
  LONG  lLastX;
  LONG  lLastY;
  ULONG ulExtraInformation;
} RAWMOUSE, *PRAWMOUSE, *LPRAWMOUSE;

typedef struct tagRAWKEYBOARD {
    USHORT MakeCode;
    USHORT Flags;
    USHORT Reserved;
    USHORT VKey;
    UINT Message;
    ULONG ExtraInformation;
} RAWKEYBOARD, *PRAWKEYBOARD, *LPRAWKEYBOARD;

typedef struct tagRAWHID {
    DWORD dwSizeHid;
    DWORD dwCount;
    BYTE bRawData;
} RAWHID, **LPRAWHID;

typedef struct tagRAWINPUT {
    RAWINPUTHEADER header;
    union {
       RAWMOUSE    mouse;
       RAWKEYBOARD keyboard;
       RAWHID      hid;
    } data;
} RAWINPUT, * PRAWINPUT, * LPRAWINPUT;

typedef UINT WINAPI ( * PGETRAWINPUTDATA ) (     
    LPRAWINPUT hRawInput,
    UINT uiCommand,
    LPVOID pData,
    PUINT pcbSize,
    UINT cbSizeHeader
);

typedef struct tagRAWINPUTDEVICELIST {
    HANDLE hDevice;
    DWORD dwType;
} RAWINPUTDEVICELIST, * PRAWINPUTDEVICELIST;

typedef UINT WINAPI ( * PGETRAWINPUTDEVICELIST ) (     
    PRAWINPUTDEVICELIST pRawInputDeviceList,
    PUINT puiNumDevices,
    UINT cbSize
);

typedef struct tagRID_DEVICE_INFO_MOUSE {
    DWORD dwId;
    DWORD dwNumberOfButtons;
    DWORD dwSampleRate;
    BOOL fHasHorizontalWheel;
} RID_DEVICE_INFO_MOUSE, * PRID_DEVICE_INFO_MOUSE;

typedef struct tagRID_DEVICE_INFO_KEYBOARD {
    DWORD dwType;
    DWORD dwSubType;
    DWORD dwKeyboardMode;
    DWORD dwNumberOfFunctionKeys;
    DWORD dwNumberOfIndicators;
    DWORD dwNumberOfKeysTotal;
} RID_DEVICE_INFO_KEYBOARD, * PRID_DEVICE_INFO_KEYBOARD;

typedef struct tagRID_DEVICE_INFO_HID {
    DWORD dwVendorId;
    DWORD dwProductId;
    DWORD dwVersionNumber;
    USHORT usUsagePage;
    USHORT usUsage;
} RID_DEVICE_INFO_HID, * PRID_DEVICE_INFO_HID;

typedef struct tagRID_DEVICE_INFO {
  DWORD    cbSize;
  DWORD    dwType;
  union {
      RID_DEVICE_INFO_MOUSE     mouse;
      RID_DEVICE_INFO_KEYBOARD  keyboard;
      RID_DEVICE_INFO_HID       hid;
  };
} RID_DEVICE_INFO, * PRID_DEVICE_INFO, * LPRID_DEVICE_INFO;

typedef UINT WINAPI ( * PGETRAWINPUTDEVICEINFO ) (     
    HANDLE hDevice,
    UINT uiCommand,
    LPVOID pData,
    PUINT pcbSize
);

HB_FUNC( REGISTERRAWINPUTDEVICES )
{
   RAWINPUTDEVICE rid[ 2 ];
   PREGISTERRAWINPUTDEVICES pRegisterRawInputDevices;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   
   pRegisterRawInputDevices = ( PREGISTERRAWINPUTDEVICES ) GetProcAddress( hUser32, "RegisterRawInputDevices" );
   
   rid[ 0 ].usUsagePage = 0x01;
   rid[ 0 ].usUsage     = 0x02; // Mouse
   rid[ 0 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 0 ].hwndTarget  = ( HWND ) hb_parnl( 1 );

   rid[ 1 ].usUsagePage = 0x01;
   rid[ 1 ].usUsage     = 0x06; // Keyboard
   rid[ 1 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 1 ].hwndTarget  = ( HWND ) hb_parnl( 1 );
   
   hb_retl( pRegisterRawInputDevices( rid, 2, sizeof( RAWINPUTDEVICE ) ) );
         
   FreeLibrary( hUser32 );
}

HB_FUNC( GETRAWINPUTDATA )
{
   UINT dwSize;
   LPBYTE lpb;
   PGETRAWINPUTDATA pGetRawInputData;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );

   pGetRawInputData = ( PGETRAWINPUTDATA ) GetProcAddress( hUser32, "GetRawInputData" );
   
   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_INPUT, NULL, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
                     
   lpb = ( LPBYTE ) hb_xgrab( dwSize );                 

   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_INPUT, lpb, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
   hb_retnl( ( unsigned long ) lpb );                 
                     
   FreeLibrary( hUser32 );
}                   

HB_FUNC( GETRAWINPUTDEVICELIST )
{
   UINT nDevices;
   PRAWINPUTDEVICELIST pRawInputDeviceList;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   PGETRAWINPUTDEVICELIST pGetRawInputDeviceList;
   PGETRAWINPUTDEVICEINFO pGetRawInputDeviceInfo;

   pGetRawInputDeviceList = ( PGETRAWINPUTDEVICELIST ) GetProcAddress( hUser32, "GetRawInputDeviceList" );
   pGetRawInputDeviceInfo = ( PGETRAWINPUTDEVICEINFO ) GetProcAddress( hUser32, "GetRawInputDeviceInfoA" );
   
   pGetRawInputDeviceList( NULL, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
   
   if( nDevices )
   {
      UINT ui;
     
      pRawInputDeviceList = ( PRAWINPUTDEVICELIST ) hb_xgrab( sizeof( RAWINPUTDEVICELIST ) * nDevices );
     
      pGetRawInputDeviceList( pRawInputDeviceList, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
     
      hb_reta( nDevices );
     
      for( ui = 0; ui < nDevices; ui++ )
      {
         char name[ 512 ];
         UINT uiSize = 511;
         
         pGetRawInputDeviceInfo( pRawInputDeviceList[ ui ].hDevice, RIDI_DEVICENAME, ( void * ) name, &uiSize );
         hb_storclen( name, uiSize, -1, ui + 1 );
      }
     
      hb_xfree( ( void * ) pRawInputDeviceList );
   }   

   FreeLibrary( hUser32 );
}

HB_FUNC( GETRAWMOUSE ) // nMouse --> hDevice
{
   UINT nDevices;
   PRAWINPUTDEVICELIST pRawInputDeviceList;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   PGETRAWINPUTDEVICELIST pGetRawInputDeviceList;
   PGETRAWINPUTDEVICEINFO pGetRawInputDeviceInfo;

   pGetRawInputDeviceList = ( PGETRAWINPUTDEVICELIST ) GetProcAddress( hUser32, "GetRawInputDeviceList" );
   pGetRawInputDeviceInfo = ( PGETRAWINPUTDEVICEINFO ) GetProcAddress( hUser32, "GetRawInputDeviceInfoA" );
   
   pGetRawInputDeviceList( NULL, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
   
   if( nDevices )
   {
      UINT ui;
      UINT uiMouses = 0;
     
      pRawInputDeviceList = ( PRAWINPUTDEVICELIST ) hb_xgrab( sizeof( RAWINPUTDEVICELIST ) * nDevices );
     
      pGetRawInputDeviceList( pRawInputDeviceList, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
     
      for( ui = 0; ui < nDevices; ui++ )
      {
         RID_DEVICE_INFO rdi;
         UINT uiSize = sizeof( RID_DEVICE_INFO );       
         UINT uiMouse = hb_parnl( 1 );
         
         rdi.cbSize = sizeof( RID_DEVICE_INFO );
         
         pGetRawInputDeviceInfo( pRawInputDeviceList[ ui ].hDevice, RIDI_DEVICEINFO, ( void * ) &rdi, &uiSize );
         
         if( rdi.dwType == RIM_TYPEMOUSE )
         {
            if( ++uiMouses == uiMouse )
            {
               hb_retnl( ( unsigned long ) pRawInputDeviceList[ ui ].hDevice );
               hb_xfree( ( void * ) pRawInputDeviceList );
               return;
            }   
         }   
      }
     
      hb_retnl( 0 );
      hb_xfree( ( void * ) pRawInputDeviceList );
   }   

   FreeLibrary( hUser32 );
}

HB_FUNC( GETRAWMOUSES ) // --> nMouses
{
   UINT nDevices;
   PRAWINPUTDEVICELIST pRawInputDeviceList;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   PGETRAWINPUTDEVICELIST pGetRawInputDeviceList;
   PGETRAWINPUTDEVICEINFO pGetRawInputDeviceInfo;
   UINT uiMouses = 0;

   pGetRawInputDeviceList = ( PGETRAWINPUTDEVICELIST ) GetProcAddress( hUser32, "GetRawInputDeviceList" );
   pGetRawInputDeviceInfo = ( PGETRAWINPUTDEVICEINFO ) GetProcAddress( hUser32, "GetRawInputDeviceInfoA" );
   
   pGetRawInputDeviceList( NULL, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
   
   if( nDevices )
   {
      UINT ui;
     
      pRawInputDeviceList = ( PRAWINPUTDEVICELIST ) hb_xgrab( sizeof( RAWINPUTDEVICELIST ) * nDevices );
     
      pGetRawInputDeviceList( pRawInputDeviceList, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
     
      for( ui = 0; ui < nDevices; ui++ )
      {
         RID_DEVICE_INFO rdi;
         UINT uiSize = sizeof( RID_DEVICE_INFO );       
         
         rdi.cbSize = sizeof( RID_DEVICE_INFO );
         
         pGetRawInputDeviceInfo( pRawInputDeviceList[ ui ].hDevice, RIDI_DEVICEINFO, ( void * ) &rdi, &uiSize );
         
         if( rdi.dwType == RIM_TYPEMOUSE )
            uiMouses++;
      }
     
      hb_xfree( ( void * ) pRawInputDeviceList );
   }   

   hb_retnl( --uiMouses ); // Windows reports an extra mouse (?)
   FreeLibrary( hUser32 );
}

#pragma ENDDUMP

Here you have the source code and EXE to test it:
http://rapidshare.com/files/148757037/rawinput.zip.html
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Antonio Linares » Sat Sep 27, 2008 9:21 am

In this new version we can select the mouse to use, though we are not processing its events yet:
Code: Select all  Expand view  RUN
// Windows Raw Input API support demo, (c) FiveTech Software

#include "FiveWin.ch"

#define WM_INPUT  0x00FF

function Main()

   local oWnd := TMyWindow():New()

   oWnd:SetText( "Raw Input test" )
   
   oWnd:hMouse = GetRawMouse( 2 ) // Use mouse 2 only
   
   RegisterRawInputDevices( oWnd:hWnd )

   oWnd:Activate()

return nil

CLASS TMyWindow FROM TWindow

   CLASSDATA lRegistered
   
   DATA hMouse // handle of the mouse to use

   METHOD HandleEvent( nMsg, nWParam, nLParam )

ENDCLASS

METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TMyWindow

   if nMsg == WM_INPUT
      if RawIsMouse( nLParam ) .and. RawGetHandle( nLParam ) == ::hMouse
         // MsgBeep()
         LogFile( "rawinput.txt", { RawIsMouse( nLParam ), RawGetHandle( nLParam ), ::hMouse } )
      endif   
   endif   

return Super:HandleEvent( nMsg, nWParam, nLParam )

#pragma BEGINDUMP

#include <hbapi.h>
#include <windows.h>

#define RIDEV_INPUTSINK 0x00000100
#define RID_INPUT       0x10000003
#define RID_HEADER      0x10000005
#define RIDI_DEVICENAME 0x20000007
#define RIDI_DEVICEINFO 0x2000000b

#define RIM_TYPEMOUSE       0
#define RIM_TYPEKEYBOARD    1
#define RIM_TYPEHID         2

typedef struct tagRAWINPUTDEVICE
{
   USHORT usUsagePage;
   USHORT usUsage;
   DWORD dwFlags;
   HWND hwndTarget;
} RAWINPUTDEVICE, * PRAWINPUTDEVICE, * LPRAWINPUTDEVICE;

typedef BOOL WINAPI ( * PREGISTERRAWINPUTDEVICES ) (     
   PRAWINPUTDEVICE pRawInputDevices,
   UINT uiNumDevices,
   UINT cbSize
);

typedef struct tagRAWINPUTHEADER {
    DWORD dwType;
    DWORD dwSize;
    HANDLE hDevice;
    WPARAM wParam;
} RAWINPUTHEADER, * PRAWINPUTHEADER;

typedef struct tagRAWMOUSE {
  USHORT    usFlags;
  union {
         ULONG    ulButtons;
             struct {
                       USHORT usButtonFlags;
                       USHORT usButtonData;
                       };
  };
  ULONG ulRawButtons;
  LONG  lLastX;
  LONG  lLastY;
  ULONG ulExtraInformation;
} RAWMOUSE, * PRAWMOUSE, * LPRAWMOUSE;

typedef struct tagRAWKEYBOARD {
    USHORT MakeCode;
    USHORT Flags;
    USHORT Reserved;
    USHORT VKey;
    UINT Message;
    ULONG ExtraInformation;
} RAWKEYBOARD, *PRAWKEYBOARD, *LPRAWKEYBOARD;

typedef struct tagRAWHID {
    DWORD dwSizeHid;
    DWORD dwCount;
    BYTE bRawData;
} RAWHID, **LPRAWHID;

typedef struct tagRAWINPUT {
    RAWINPUTHEADER header;
    union {
       RAWMOUSE    mouse;
       RAWKEYBOARD keyboard;
       RAWHID      hid;
    } data;
} RAWINPUT, * PRAWINPUT, * LPRAWINPUT;

typedef UINT WINAPI ( * PGETRAWINPUTDATA ) (     
    LPRAWINPUT hRawInput,
    UINT uiCommand,
    LPVOID pData,
    PUINT pcbSize,
    UINT cbSizeHeader
);

typedef struct tagRAWINPUTDEVICELIST {
    HANDLE hDevice;
    DWORD dwType;
} RAWINPUTDEVICELIST, * PRAWINPUTDEVICELIST;

typedef UINT WINAPI ( * PGETRAWINPUTDEVICELIST ) (     
    PRAWINPUTDEVICELIST pRawInputDeviceList,
    PUINT puiNumDevices,
    UINT cbSize
);

typedef struct tagRID_DEVICE_INFO_MOUSE {
    DWORD dwId;
    DWORD dwNumberOfButtons;
    DWORD dwSampleRate;
    BOOL fHasHorizontalWheel;
} RID_DEVICE_INFO_MOUSE, * PRID_DEVICE_INFO_MOUSE;

typedef struct tagRID_DEVICE_INFO_KEYBOARD {
    DWORD dwType;
    DWORD dwSubType;
    DWORD dwKeyboardMode;
    DWORD dwNumberOfFunctionKeys;
    DWORD dwNumberOfIndicators;
    DWORD dwNumberOfKeysTotal;
} RID_DEVICE_INFO_KEYBOARD, * PRID_DEVICE_INFO_KEYBOARD;

typedef struct tagRID_DEVICE_INFO_HID {
    DWORD dwVendorId;
    DWORD dwProductId;
    DWORD dwVersionNumber;
    USHORT usUsagePage;
    USHORT usUsage;
} RID_DEVICE_INFO_HID, * PRID_DEVICE_INFO_HID;

typedef struct tagRID_DEVICE_INFO {
  DWORD    cbSize;
  DWORD    dwType;
  union {
      RID_DEVICE_INFO_MOUSE     mouse;
      RID_DEVICE_INFO_KEYBOARD  keyboard;
      RID_DEVICE_INFO_HID       hid;
  };
} RID_DEVICE_INFO, * PRID_DEVICE_INFO, * LPRID_DEVICE_INFO;

typedef UINT WINAPI ( * PGETRAWINPUTDEVICEINFO ) (     
    HANDLE hDevice,
    UINT uiCommand,
    LPVOID pData,
    PUINT pcbSize
);

HB_FUNC( REGISTERRAWINPUTDEVICES )
{
   RAWINPUTDEVICE rid[ 2 ];
   PREGISTERRAWINPUTDEVICES pRegisterRawInputDevices;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   
   pRegisterRawInputDevices = ( PREGISTERRAWINPUTDEVICES ) GetProcAddress( hUser32, "RegisterRawInputDevices" );
   
   rid[ 0 ].usUsagePage = 0x01;
   rid[ 0 ].usUsage     = 0x02; // Mouse
   rid[ 0 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 0 ].hwndTarget  = ( HWND ) hb_parnl( 1 );

   rid[ 1 ].usUsagePage = 0x01;
   rid[ 1 ].usUsage     = 0x06; // Keyboard
   rid[ 1 ].dwFlags     = RIDEV_INPUTSINK;
   rid[ 1 ].hwndTarget  = ( HWND ) hb_parnl( 1 );
   
   hb_retl( pRegisterRawInputDevices( rid, 2, sizeof( RAWINPUTDEVICE ) ) );
         
   FreeLibrary( hUser32 );
}

HB_FUNC( GETRAWINPUTDATA )
{
   UINT dwSize;
   LPBYTE lpb;
   PGETRAWINPUTDATA pGetRawInputData;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );

   pGetRawInputData = ( PGETRAWINPUTDATA ) GetProcAddress( hUser32, "GetRawInputData" );
   
   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_INPUT, NULL, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
                     
   lpb = ( LPBYTE ) hb_xgrab( dwSize );                 

   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_INPUT, lpb, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
   hb_retnl( ( unsigned long ) lpb );                 
                     
   FreeLibrary( hUser32 );
}                   

HB_FUNC( GETRAWINPUTDEVICELIST )
{
   UINT nDevices;
   PRAWINPUTDEVICELIST pRawInputDeviceList;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   PGETRAWINPUTDEVICELIST pGetRawInputDeviceList;
   PGETRAWINPUTDEVICEINFO pGetRawInputDeviceInfo;

   pGetRawInputDeviceList = ( PGETRAWINPUTDEVICELIST ) GetProcAddress( hUser32, "GetRawInputDeviceList" );
   pGetRawInputDeviceInfo = ( PGETRAWINPUTDEVICEINFO ) GetProcAddress( hUser32, "GetRawInputDeviceInfoA" );
   
   pGetRawInputDeviceList( NULL, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
   
   if( nDevices )
   {
      UINT ui;
     
      pRawInputDeviceList = ( PRAWINPUTDEVICELIST ) hb_xgrab( sizeof( RAWINPUTDEVICELIST ) * nDevices );
     
      pGetRawInputDeviceList( pRawInputDeviceList, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
     
      hb_reta( nDevices );
     
      for( ui = 0; ui < nDevices; ui++ )
      {
         char name[ 512 ];
         UINT uiSize = 511;
         
         pGetRawInputDeviceInfo( pRawInputDeviceList[ ui ].hDevice, RIDI_DEVICENAME, ( void * ) name, &uiSize );
         hb_storclen( name, uiSize, -1, ui + 1 );
      }
     
      hb_xfree( ( void * ) pRawInputDeviceList );
   }   

   FreeLibrary( hUser32 );
}

HB_FUNC( GETRAWMOUSE ) // nMouse --> hDevice
{
   UINT nDevices;
   PRAWINPUTDEVICELIST pRawInputDeviceList;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   PGETRAWINPUTDEVICELIST pGetRawInputDeviceList;
   PGETRAWINPUTDEVICEINFO pGetRawInputDeviceInfo;

   pGetRawInputDeviceList = ( PGETRAWINPUTDEVICELIST ) GetProcAddress( hUser32, "GetRawInputDeviceList" );
   pGetRawInputDeviceInfo = ( PGETRAWINPUTDEVICEINFO ) GetProcAddress( hUser32, "GetRawInputDeviceInfoA" );
   
   pGetRawInputDeviceList( NULL, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
   
   if( nDevices )
   {
      UINT ui;
      UINT uiMouses = 0;
     
      pRawInputDeviceList = ( PRAWINPUTDEVICELIST ) hb_xgrab( sizeof( RAWINPUTDEVICELIST ) * nDevices );
     
      pGetRawInputDeviceList( pRawInputDeviceList, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
     
      for( ui = 0; ui < nDevices; ui++ )
      {
         RID_DEVICE_INFO rdi;
         UINT uiSize = sizeof( RID_DEVICE_INFO );       
         UINT uiMouse = hb_parnl( 1 );
         
         rdi.cbSize = sizeof( RID_DEVICE_INFO );
         
         pGetRawInputDeviceInfo( pRawInputDeviceList[ ui ].hDevice, RIDI_DEVICEINFO, ( void * ) &rdi, &uiSize );
         
         if( rdi.dwType == RIM_TYPEMOUSE )
         {
            if( ++uiMouses == uiMouse )
            {
               hb_retnl( ( unsigned long ) pRawInputDeviceList[ ui ].hDevice );
               hb_xfree( ( void * ) pRawInputDeviceList );
               return;
            }   
         }   
      }
     
      hb_retnl( 0 );
      hb_xfree( ( void * ) pRawInputDeviceList );
   }   

   FreeLibrary( hUser32 );
}

HB_FUNC( GETRAWMOUSES ) // --> nMouses
{
   UINT nDevices;
   PRAWINPUTDEVICELIST pRawInputDeviceList;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );
   PGETRAWINPUTDEVICELIST pGetRawInputDeviceList;
   PGETRAWINPUTDEVICEINFO pGetRawInputDeviceInfo;
   UINT uiMouses = 0;

   pGetRawInputDeviceList = ( PGETRAWINPUTDEVICELIST ) GetProcAddress( hUser32, "GetRawInputDeviceList" );
   pGetRawInputDeviceInfo = ( PGETRAWINPUTDEVICEINFO ) GetProcAddress( hUser32, "GetRawInputDeviceInfoA" );
   
   pGetRawInputDeviceList( NULL, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
   
   if( nDevices )
   {
      UINT ui;
     
      pRawInputDeviceList = ( PRAWINPUTDEVICELIST ) hb_xgrab( sizeof( RAWINPUTDEVICELIST ) * nDevices );
     
      pGetRawInputDeviceList( pRawInputDeviceList, &nDevices, sizeof( RAWINPUTDEVICELIST ) );
     
      for( ui = 0; ui < nDevices; ui++ )
      {
         RID_DEVICE_INFO rdi;
         UINT uiSize = sizeof( RID_DEVICE_INFO );       
         
         rdi.cbSize = sizeof( RID_DEVICE_INFO );
         
         pGetRawInputDeviceInfo( pRawInputDeviceList[ ui ].hDevice, RIDI_DEVICEINFO, ( void * ) &rdi, &uiSize );
         
         if( rdi.dwType == RIM_TYPEMOUSE )
            uiMouses++;
      }
     
      hb_xfree( ( void * ) pRawInputDeviceList );
   }   

   hb_retnl( --uiMouses ); // Windows reports an extra mouse (?)
   FreeLibrary( hUser32 );
}

HB_FUNC( RAWISMOUSE )
{
   LPRAWINPUT pri = ( LPRAWINPUT ) hb_parnl( 1 );
   UINT dwSize;
   LPBYTE lpb;
   PGETRAWINPUTDATA pGetRawInputData;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );

   pGetRawInputData = ( PGETRAWINPUTDATA ) GetProcAddress( hUser32, "GetRawInputData" );
   
   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_HEADER, NULL, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
                     
   lpb = ( LPBYTE ) hb_xgrab( dwSize );                 

   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_HEADER, lpb, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
   
   hb_retl( ( ( PRAWINPUTHEADER ) lpb )->dwType == RIM_TYPEMOUSE );
   hb_xfree( ( void * ) lpb );
   
   FreeLibrary( hUser32 );
}   

HB_FUNC( RAWGETHANDLE )
{
   LPRAWINPUT pri = ( LPRAWINPUT ) hb_parnl( 1 );
   UINT dwSize;
   LPBYTE lpb;
   PGETRAWINPUTDATA pGetRawInputData;
   HINSTANCE hUser32 = LoadLibrary( "user32.dll" );

   pGetRawInputData = ( PGETRAWINPUTDATA ) GetProcAddress( hUser32, "GetRawInputData" );
   
   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_HEADER, NULL, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
                     
   lpb = ( LPBYTE ) hb_xgrab( dwSize );                 

   pGetRawInputData( ( LPRAWINPUT ) hb_parnl( 1 ), RID_HEADER, lpb, &dwSize,
                     sizeof( RAWINPUTHEADER ) );
   
   hb_retnl( ( unsigned long ) ( ( PRAWINPUTHEADER ) lpb )->hDevice );
   hb_xfree( ( void * ) lpb );
   
   FreeLibrary( hUser32 );
}   

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot], Timm Sodtalbers and 52 guests