-2

My requirement is to get keyspressed when user opens specific URL in chrome. For that I am firstly checking for URL opened in chrome after that I am setting hook using below code

    _hookID = SetHook(_proc);
    Application.Run();
    UnhookWindowsHookEx(_hookID);

it is getting the keys on that URL but after that if i close that URL it is continuously getting the keys, that's what i don't want. It is because I am calling application.run(). How to run the code after application.run()? I think that would be the solution

James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

-3

The call to Aplication.Run is blocking as it starts the Windows Message loop. That call will return once the main window gets closed and/or one of the messages (for example WM_QUIT) that will end a windows program is being received and handled by the message loop

If you want to run code after the Run method has been called, one option would be to register a handler on the Application.Idle event

_hookID = SetHook(_proc);
Application.Idle += (s,e) => { UnhookWindowsHookEx(_hookID);};
Application.Run();

In my testing the Idle event is raised, once the Run method is called an no other messages are handled by the main message loop.

Alternatively gracefully exit your windows loop, for example by posting WM_QUIT to its top window.

rene
  • 41,474
  • 78
  • 114
  • 152
  • Hey Rene, what it doing is when i open secipic URL it is binding the keylogger event only for one time, so it is not working when i pressed second key on that URL but i want to capture whole word – abhishekagrwl25 Feb 06 '18 at 09:49
  • i have changed it for whole word, it is capturing whole word , all good, can you pleae refer what to use for the next time i open the URL it will capture the same. – abhishekagrwl25 Feb 06 '18 at 10:28