no wm_input message until other messages are being sent lazarus / 34-Windows ---------------------------------------------------------------------- dmudie · 2016-11-14 17:05 I have progressed with reading the USB Key Pedal system.  I have registered the raw device, and have set up a message loop that handles wm_input.  I am able to read the raw data and determine which pedals are pressed and which ones are released.  All good, and thanks for the help getting to this point.  The problem I am having now is that the wm_input message is not being processed by my message loop until other messages (events) are being received.  For instance, I can click on one of the foot pedals, and the wm_input message is not sent/received until I do something else on the window such as drag the window across.  A simple work around is to set up a timer and have it enabled.  I don't even have to do anything with the event.  Just having the event triggered causes the wm_import messages to be received. Any insights on what is going on / how to properly handle this would be appreciated.  I'm sure my workaround is not the best way of dealing with it. Thanks, David Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit umain; {$mode OBJFPC}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  ExtCtrls, jwawinuser, windows; const  BUFFER_LENGTH =              255;  type   { TForm1 }   TForm1 = class(TForm)    bRegisterDevice: TButton;    cbLeft: TCheckBox;    cbMiddle: TCheckBox;    cbRight: TCheckBox;    Memo1: TMemo;    Panel1: TPanel;    Timer1: TTimer;     procedure bRegisterDeviceClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure Timer1Timer(Sender: TObject);  private    { private declarations }   public    { public declarations }    function WMInput(wParam: WParam; lParam: LParam):LRESULT;  end; var  Form1: TForm1; implementation {$R *.lfm}  var  PrevWndProc:windows.WNDPROC;     function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam;  lParam: LParam): LRESULT; stdcall;  // replace the message loop in order to handle the WM_INPUT messagebegin  case uMsg of  WM_INPUT:    begin//      Form1.Memo1.Lines.Add('Beep');       result := Form1.WMInput(wParam,lParam); //     result := DefWindowProc(Ahwnd,uMsg,wParam,LParam);     end;  end;   // if the message is not a WM_INPUT, then call the default message loop to handle it  Result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);end;  { TForm1 }  function TForm1.WMInput(wParam: WParam; lParam: LParam):LRESULT;var//  pBuffer     : ^RAWINPUT;  pBuffer     : ^char;  lwBufferLength: LongWord;  iBytesCopied: integer;   cButtonStatus: char; begin  Result := 0;  Memo1.Lines.Add('WM_INPUT');  Memo1.Lines.Add('LParam : ' + IntToStr(LParam));  Memo1.Lines.Add('WParam : ' + IntToStr(WParam));   lwBufferLength := SizeOf(RAWINPUTHEADER); //  if GetRawInputData(hToRawInput,RID_INPUT,pBuffer,lwBufferLength,0) <> 0 then  if GetRawInputData(HRAWINPUT(lParam),RID_INPUT,NIL,lwBufferLengt h,SizeOf(RAWINPUTHEADER)) <> 0 then     begin       Memo1.Lines.Add('Error Getting Buffer Size GetRawInputData(hToRawInput,RID_HEADER,pBuffer,lwBufferLength,0)');     exit;     end;    Memo1.Lines.Add('Header Buffer Size is ' + IntToStr(lwBufferLength));   GetMem(pBuffer,lwBufferLength);   try    iBytesCopied := GetRawInputData(HRAWINPUT(lParam),RID_INPUT,pBuffer,l wBufferLength,SizeOf(RAWINPUTHEADER));     Memo1.Lines.Add('Bytes Copied : ' + IntToStr(iBytesCopied));      if  iBytesCopied < 1 then   begin       Memo1.Lines.Add('Error Getting Header GetRawInputData(hToRawInput,RID_HEADER,pBuffer,lwBufferLength,0)');     exit;     end;    cButtonStatus := (pBuffer+25)^;  Memo1.Lines.Add('Button Status: ' + IntToStr(integer(cButtonStatus)));    if (integer(cButtonStatus) and 1) <> 0 then     cbLeft.checked := true  else     cbLeft.checked := false;   if (integer(cButtonStatus) and 2) <> 0 then     cbMiddle.checked := true  else     cbMiddle.checked := false;   if (integer(cButtonStatus) and 4) <> 0 then     cbRight.checked := true  else     cbRight.checked := false;    finally     FreeMem(pBuffer);   end;  end; procedure TForm1.FormCreate(Sender: TObject);begin    Prev WndProc:=Windows.WNDPROC(SetWindowLongPtr(Self.Handle,GWL_WNDPROC,PtrI nt(@WndCallback)));end; procedure TForm1.Timer1Timer(Sender: TObject);begin  // Don't need to do anything, just having an event is enough to cause the  // wm_input messages to be received.end;   procedure TForm1.bRegisterDeviceClick(Sender: TObject);var  nDevices    : LongWord;  listentries : Array of RAWINPUTDEVICELIST;  i           : integer;  pBuffer     : ^char;  lwBufferLength: LongWord;  iReturnedByteCount:integer;   pRID_DEVICE_INFO : ^RID_DEVICE_INFO;  lwRID_DEVICE_INFO_Length : LongWord;     pRAWINPUTDEVICE : ^RAWINPUTDEVICE; begin  lwBufferLength := BUFFER_LENGTH;   // use a hardcoded length here because it's an easy way to handle the string for the device name (length not known ahead of time).  GetMem(pBuffer,lwBufferLength);   lwRID_DEVICE_INFO_Length := sizeof(RID_DEVICE_INFO);  Memo1.Lines.Add('lwRID_DEVICE_INFO_Length : ' + IntToStr(lwRID_DEVICE_INFO_Length));  GetMem(pRID_DEVICE_INFO, lwRID_DEVICE_INFO_Length);   Memo1.Lines.Add('get information about list');  if (GetRawInputDeviceList(nil, nDevices, sizeof(RAWINPUTDEVICELIST)) = 0) then  begin    Memo1.Lines.Add('found ' + IntToStr(nDevices) + ' devices');     Memo1.Lines.Add('Set size of list');    SetLength(listentries, nDevices);     if (GetRawInputDeviceList(@ListEntries[0], nDevices, sizeof(RAWINPUTDEVICELIST)) <> LongWord(-1) ) then    begin      for i := low(ListEntries) to High(ListEntries) do      begin        Memo1.Lines.Add('Line = ' + IntToStr(i) + '  handle = ' + IntToStr(listentries[i].hDevice) + '  Type = ' + IntToStr(listentries[i].dwType));         case listentries[i].dwType of        RIM_TYPEHID:               Memo1.Lines.Add('Other');        RIM_TYPEKEYBOARD:          Memo1.Lines.Add('Keyboard');        RIM_TYPEMOUSE:             Memo1.Lines.Add('Mouse');        end;       iReturnedByteCount := GetRawInputDeviceInfo(listentries[i].hDevice,R IDI_DEVICENAME,pBuffer,lwBufferLength);        if (iReturnedByteCount < 1) then           begin             Memo1.Lines.Add('Error: Buffer too small : RIDI_DEVICENAME');             exit;           end;      Memo1.Lines.Add(pBuffer + ' : ' + IntToStr(iReturnedByteCount));        iReturnedByteCount := GetRawInputDeviceInfo(listentries[i].hDevice, RIDI_DEVICEINFO,pRID_DEVICE_INFO,lwRID_DEVICE_INFO_Length);        if (iReturnedByteCount < 1) then           begin             Memo1.Lines.Add('Error: Buffer to small : RIDI_DEVICEINFO');           exit;           end;        Memo1.Lines.Add('RID_DEVICE_INFO size : ' + IntToStr(pRID_DEVICE_INFO^.cbSize));         case pRID_DEVICE_INFO^.dwType of        RIM_TYPEHID:          begin          Memo1.Lines.Add('dwType : RIM_TYPEHID');            Memo1.Lines.Add('dwVendorId : ' + IntToStr(pRID_DEVICE_INFO^.hid.dwVendorId));            Memo1.Lines.Add('dwProductId : ' + IntToStr(pRID_DEVICE_INFO^.hid.dwProductId));            Memo1.Lines.Add('dwVersionNumber : ' + IntToStr(pRID_DEVICE_INFO^.hid.dwVersionNumber));            Memo1.Lines.Add('usUsagePage : ' + IntToStr(pRID_DEVICE_INFO^.hid.usUsagePage));            Memo1.Lines.Add('usUsage : ' + IntToStr(pRID_DEVICE_INFO^.hid.usUsage));          end;        RIM_TYPEKEYBOARD:          begin            Memo1.Lines.Add('dwType : RIM_TYPEKEYBOARD');           end;        RIM_TYPEMOUSE:          begin            Memo1.Lines.Add('dwType : RIM_TYPEMOUSE');           end;         end;         if listentries[i].dwType = RIM_TYPEHID then           if pRID_DEVICE_INFO^.hid.dwVendorId = 1523 then       // vendor id for infinity              if pRID_DEVICE_INFO^.hid.dwProductId = 255 then    // product id for usb foot pedal                 begin                   Memo1.Lines.Add('******************************************');                 Memo1.Lines.Add('Found It');                   Memo1.Lines.Add('******************************************');                  GetMem(pRAWINPUTDEVICE,sizeof(RAWINPUTDEVICE));                  pRAWINPUTDEVICE^.usUsagePage   := pRID_DEVICE_INFO^.hid.usUsagePage;                   pRAWINPUTDEVICE^.usUsage       := pRID_DEVICE_INFO^.hid.usUsage;                 pRAWINPUTDEVICE^.dwFlags       := 0;                   pRAWINPUTDEVICE^.hwndTarget    := self.Handle;                    if not RegisterRawInputDevices(pRAWINPUTDEVICE,1,sizeof(RAWINPUTDEVICE))then                      begin                        Memo1.Lines.Add('Failed To Register HID');                        exit;                      end;                         FreeMem(pRAWINPUTDEVICE);                    break;                 end;               Memo1.Lines.Add(' ');        end;    end    else Memo1.Lines.Add('error retreiving the list');     SetLength(listentries, 0);  end  else Memo1.Lines.Add('error');   FreeMem(pBuffer);  FreeMem(pRID_DEVICE_INFO);end;  end. ---------------------------------------------------------------------- ASerge · 2016-11-14 17:47 Quote from: dmudie on November 14, 2016, 06:05:59 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //...                 pRAWINPUTDEVICE^.dwFlags       := 0;                   pRAWINPUTDEVICE^.hwndTarget    := self.Handle;//...Try pRAWINPUTDEVICE^.dwFlags := RIDEV_INPUTSINK; ---------------------------------------------------------------------- Remy Lebeau · 2016-11-14 22:25 Quote from: dmudie on November 14, 2016, 06:05:59 pmI have registered the raw device, and have set up a message loop that handles wm_input. Why are you using SetWindowLongPtr() to subclass the Form's window?  You should be overriding the Form's virtual WndProc() method instead. You should also override your Form's virtual InitializedWnd() method to call RegisterRawInputDevices().  That way, you can (re)register your current window whenever it is (re)created.  If your window ever gets recreated, you need to reregister the new window handle in order to continue receiving messages. Quote from: dmudie on November 14, 2016, 06:05:59 pmThe problem I am having now is that the wm_input message is not being processed by my message loop until other messages (events) are being received.  That implies that you are not managing your message loop correctly.  But you did not show that code. Quote from: dmudie on November 14, 2016, 06:05:59 pmCode: [Select]pRAWINPUTDEVICE^.dwFlags       := 0; By setting the dwFlags to 0, you will receive WM_INPUT messages only when your Form's window is in the foreground.  If you need to receive messages while the window is not in the foreground, you need to enable the RIDEV_INPUTSINK flag. You should also consider enabling the RIDEV_DEVNOTIFY flag so you can receive WM_INPUT_DEVICE_CHANGE messages. ---------------------------------------------------------------------- dmudie · 2016-11-15 15:36 ASerge and Remy, Thank you for your responses.  I did look at the dwflags parameter, but was having other problems.  I never got back to reviewing them.  I tried using the RIDEV_INPUTSINK setting that you both suggested, but it did not change the behavior.  I will read through the documentation and look at all of the dwflag settings to see if I have missed something else. Remy: Thank you for your suggestion about using WndProc().  From my years ago Delphi days I was thinking about going this way... however, in doing research I found two posts.  Starting with http://forum.lazarus.freepascal.org/index.php?topic=6062.0, Vincent describes having to hook the WndProc yourself.  I couldn't find any other references to this.  He also gives a link in the wiki that describes using the windows SetWindowLong function.  I didn't get this working, but I found some sample code on a Delphi site that used SetWindowLongPtr, and it worked.  I would love to override WndProc.  It seems a lot cleaner.  I tried doing this in response to your message, but it didn't work.  Could you please explain what I am doing wrong or point me to some sample code? Thanks, David. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  unit umain; {$mode OBJFPC}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,  ExtCtrls, jwawinuser, windows, LMessages; const  BUFFER_LENGTH =              255;  type   { TForm1 }   TForm1 = class(TForm)    bRegisterDevice: TButton;    cbLeft: TCheckBox;    cbMiddle: TCheckBox;    cbRight: TCheckBox;    Memo1: TMemo;    Panel1: TPanel;    Timer1: TTimer;     procedure bRegisterDeviceClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure Timer1Timer(Sender: TObject);  private    { private declarations }   protected    procedure WndProc ( var Msg: TLMessage ) ; override;   public    { public declarations }    function WMInput(wParam: WParam; lParam: LParam):LRESULT;  end; var  Form1: TForm1; implementation {$R *.lfm}  //var//  PrevWndProc:windows.WNDPROC;   {function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam;  lParam: LParam): LRESULT; stdcall;  // replace the message loop in order to handle the WM_INPUT messagebegin  case uMsg of  WM_INPUT:    begin//      Form1.Memo1.Lines.Add('Beep');       result := Form1.WMInput(wParam,lParam); //     result := DefWindowProc(Ahwnd,uMsg,wParam,LParam);     end;  end;   // if the message is not a WM_INPUT, then call the default message loop to handle it  Result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);end;  }  { TForm1 }  procedure TForm1.WndProc ( var Msg: TLMessage ) ;begin  if Msg.msg = WM_INPUT then     begin       Msg.Result := WMInput(Msg.wParam,Msg.lParam);     end;   inherited WndProc(Msg);end; function TForm1.WMInput(wParam: WParam; lParam: LParam):LRESULT;var//  pBuffer     : ^RAWINPUT;  pBuffer     : ^char;  lwBufferLength: LongWord;  iBytesCopied: integer;   cButtonStatus: char; begin  Result := 0;   lwBufferLength := SizeOf(RAWINPUTHEADER);   if GetRawInputData(HRAWINPUT(lParam),RID_INP UT,NIL,lwBufferLength,SizeOf(RAWINPUTHEADER)) <> 0 then     begin     Memo1.Lines.Add('Error Getting Buffer Size GetRawInputData(hToRawInput,RID_HEADER,pBuffer,lwBufferLength,0)');     exit;     end;    GetMem(pBuffer,lwBufferLength);   try      iBytesCopied := GetRawInputData(HRAWINPUT(lParam),RID_INPUT,pBuffer,l wBufferLength,SizeOf(RAWINPUTHEADER));      if  iBytesCopied < 1 then     begin       exit;     end;    cButtonStatus := (pBuffer+25)^;  Memo1.Lines.Add('Button Status: ' + IntToStr(integer(cButtonStatus)));    if (integer(cButtonStatus) and 1) <> 0 then     cbLeft.checked := true  else     cbLeft.checked := false;   if (integer(cButtonStatus) and 2) <> 0 then     cbMiddle.checked := true  else     cbMiddle.checked := false;   if (integer(cButtonStatus) and 4) <> 0 then     cbRight.checked := true  else     cbRight.checked := false;    finally     FreeMem(pBuffer);   end;  end; procedure TForm1.FormCreate(Sender: TObject);begin//    Pr evWndProc:=Windows.WNDPROC(SetWindowLongPtr(Self.Handle,GWL_WNDPROC,Pt rInt(@WndCallback)));end; procedure TForm1.Timer1Timer(Sender: TObject);begin  // Don't need to do anything, just having an event is enough to cause the  // wm_input messages to be received.end;   procedure TForm1.bRegisterDeviceClick(Sender: TObject);var  nDevices    : LongWord;  listentries : Array of RAWINPUTDEVICELIST;  i           : integer;  pBuffer     : ^char;  lwBufferLength: LongWord;  iReturnedByteCount:integer;   pRID_DEVICE_INFO : ^RID_DEVICE_INFO;  lwRID_DEVICE_INFO_Length : LongWord;     pRAWINPUTDEVICE : ^RAWINPUTDEVICE; begin  lwBufferLength := BUFFER_LENGTH;   // use a hardcoded length here because it's an easy way to handle the string for the device name (length not known ahead of time).  GetMem(pBuffer,lwBufferLength);   lwRID_DEVICE_INFO_Length := sizeof(RID_DEVICE_INFO);  Memo1.Lines.Add('lwRID_DEVICE_INFO_Length : ' + IntToStr(lwRID_DEVICE_INFO_Length));  GetMem(pRID_DEVICE_INFO, lwRID_DEVICE_INFO_Length);   Memo1.Lines.Add('get information about list');  if (GetRawInputDeviceList(nil, nDevices, sizeof(RAWINPUTDEVICELIST)) = 0) then  begin    Memo1.Lines.Add('found ' + IntToStr(nDevices) + ' devices');     Memo1.Lines.Add('Set size of list');    SetLength(listentries, nDevices);     if (GetRawInputDeviceList(@ListEntries[0], nDevices, sizeof(RAWINPUTDEVICELIST)) <> LongWord(-1) ) then    begin      for i := low(ListEntries) to High(ListEntries) do      begin         iReturnedByteCount := GetRawInputDeviceInfo(listentries[i].hDevice,RID I_DEVICENAME,pBuffer,lwBufferLength);        if (iReturnedByteCount < 1) then           begin             Memo1.Lines.Add('Error: Buffer too small : RIDI_DEVICENAME');             exit;           end;        Memo1.Lines.Add(pBuffer + ' : ' + IntToStr(iReturnedByteCount));        iReturnedByteCount := GetRawInputDeviceInfo(listentries[i].hDevice, RIDI_DEVICEINFO,pRID_DEVICE_INFO,lwRID_DEVICE_INFO_Length);        if (iReturnedByteCount < 1) then           begin             Memo1.Lines.Add('Error: Buffer to small : RIDI_DEVICEINFO');           exit;           end;         if listentries[i].dwType = RIM_TYPEHID then           if pRID_DEVICE_INFO^.hid.dwVendorId = 1523 then       // vendor id for infinity              if pRID_DEVICE_INFO^.hid.dwProductId = 255 then    // product id for usb foot pedal                 begin                   Memo1.Lines.Add('******************************************');                 Memo1.Lines.Add('Found It');                   Memo1.Lines.Add('******************************************');                  GetMem(pRAWINPUTDEVICE,sizeof(RAWINPUTDEVICE));                  pRAWINPUTDEVICE^.usUsagePage   := pRID_DEVICE_INFO^.hid.usUsagePage;                   pRAWINPUTDEVICE^.usUsage       := pRID_DEVICE_INFO^.hid.usUsage;//                 pRAWINPUTDEVICE^.dwFlags       := 0;                   pRAWINPUTDEVICE^.dwFlags := RIDEV_INPUTSINK;                   pRAWINPUTDEVICE^.hwndTarget    := self.Handle;                    if not RegisterRawInputDevices(pRAWINPUTDEVICE,1,sizeof(RAWINPUTDEVICE))then                      begin                        Memo1.Lines.Add('Failed To Register HID');                        exit;                      end;                     FreeMem(pRAWINPUTDEVICE);                    break;                 end;               Memo1.Lines.Add(' ');        end;    end    else Memo1.Lines.Add('error retreiving the list');     SetLength(listentries, 0);  end  else Memo1.Lines.Add('error');   FreeMem(pBuffer);  FreeMem(pRID_DEVICE_INFO);end;  end. ---------------------------------------------------------------------- Remy Lebeau · 2016-11-15 19:19 Quote from: dmudie on November 15, 2016, 04:36:19 pmThank you for your suggestion about using WndProc().  From my years ago Delphi days I was thinking about going this way... however, in doing research I found two posts.  Starting with http://forum.lazarus.freepascal.org/index.php?topic=6062.0, Vincent describes having to hook the WndProc yourself. At the very bottom of that same discussion, oxen said: Quote from: oxen on February 18, 2011, 01:33:37 pmIn the version of Lazarus that I am using (0.9.30 FPC 2.5.1 SVN 29251) custom windows messages seem to be working the Delphi way And he shows an example using the 'message' directive to handle WM_HOTKEY.  The same kind of thing that mauzer_tim documented as not working in 0.9.26. So, try seeing if applying the 'message' directive to your WMInput() method can receive the WM_INPUT messages. On the other hand, I see posts that say only a few Window messages can be caught with the 'message' directive, so maybe WM_INPUT will not work that way after all. Quote from: dmudie on November 15, 2016, 04:36:19 pmI didn't get this working, but I found some sample code on a Delphi site that used SetWindowLongPtr, and it worked.  If you are going to resort to subclassing the window procedure, at the very least you should use SetWindowSubclass() instead (see Safer subclassing). Either way, like I said earlier, the OnCreate event is a bad place to perform the subclassing.  Try overriding InitializedWnd() instead.  That is also a good place to call RegisterRawInputDevices() as well. ---------------------------------------------------------------------- dmudie · 2016-11-22 15:10 Okay, it's taken me a while to get things working.  Thanks for all of your help.