Hello!

I have a violation of the hook on the mouse at the stage of its creation. I found out the problem, but how to solve, I can not understand.

In general, the thing is this: there is a program in which there is a button, by clicking on which a trap is placed on the mouse and keyboard. Everything works with the keyboard, but the problem with the mouse. As it turned out from the analysis of the work, the hook is broken when, during its creation, the actions that it tracks are performed. That is, if you click on this button in this program itself and start moving it with your mouse (that is, triggering the onmousemove event), the hook will break. If you click on this button, remove your hands from the mouse for a few seconds and then start working, then order with a hook! What can be wrong? Thank you ... and sorry for such a long notation.

Here is the hook handling function:

 function wmhook(nCode: Integer; WParam: WPARAM; LParam: LPARAM):LRESULT; stdcall; begin if wparam = WM_LBUTTONDOWN then begin windows.beep(1000, 100); // actions end; if wparam = WM_RBUTTONDOWN then begin windows.beep(2000, 100); // other actions end; Result := CallNextHookEx(hh, nCode, WParam, LParam); end; 

Function BlockInput(BOOL); from USER32.DLL saved!

    2 answers 2

    Why don't you have a check for nCode <0? The MSDN is clearly written:

    If the code is not less than zero, it would be necessary to make a call.

    Those. if nCode <0, there should be no further processing, except for returning the result of the call to CallNextHookEx.

    And it also says:

    It can be used to return the procedure.

    Those. you can (need) analyze LPARAM, which indicates MOUSEHOOKSTRUCT, look at which window the event should be sent and, if necessary, prevent it.

      Here is a piece of my code from the program. Also work with hooks, see if you can redo your subroutine. And the question is, why are two conditions for the same event pressing the mouse button?

        procedure TForm1.WMICON(var msg: TMessage); var P : TPoint; begin case msg.LParam of WM_LBUTTONDOWN: begin GetCursorPos(p); SetForegroundWindow(Application.MainForm.Handle); PopupMenu1.Popup(PX, PY); end; WM_LBUTTONDBLCLK : N1Click(Self); end; end; 
      • one
        I have two completely different conditions for the hook: for clicking the left and right mouse buttons! Be a bit closer =) And your code is inappropriate here! Your code handles the event when an application icon minimized to the system tray is clicked or double clicked! For example, a single click pops up a menu, and a double click takes place on the main menu ... I don’t understand why you put out your code ..... - AseN
      • All noticed that different keys are processing. I posted my code, thinking that I do not cook at all in this matter. Worked with hukami, with the same as you have not encountered a problem. - IntegralAL
      • And what, you can now lay out everything that comes to mind? Why did you not put out one of the volumes of L.N. Tolstoy "War and Peace"? - AseN