0

i have written a small form application, which contains textbox only. I have enabled shortcut key by using low level keyboard hook to give focus to application when needed. All is working fine, I press the short key i.e. (Left Control Key)+(Left Control Key) for the first time the application get proper control (focus). But when I deactivate and redo the shortcut key, the icon of the application on the taskbar starts blinking and the form doesn't get focus, the title bar is greyed out.

More Information :

  1. on deactivation the form's opacity is reduced but it remains on top, so the whole time the form is displayed on the screen
  2. This form is activated from another class, within the application.
  3. On Activation event opacity of the form is increased so it now very well visible

I am giving focus to the application by using form.Activate() I have also tried from.Visible but with no luck. The activation works for the first time only, post that the icon in taskbar blinks.

Does any have any idea why is this happening?

svick
  • 236,525
  • 50
  • 385
  • 514
vikramaditya234
  • 1,278
  • 2
  • 18
  • 37
  • This is normal, you cannot steal the focus away from another app. You should have used RegisterHotKey() instead of the hook. – Hans Passant Feb 07 '12 at 11:12

1 Answers1

1

This answer applies to you as well: https://stackoverflow.com/a/3789985/64121 . You need to make use of the AttachThreadInput API function in order to steal focus away from another app.

Community
  • 1
  • 1
Dan Byström
  • 9,067
  • 5
  • 38
  • 68
  • Thanks the solution pointed by you worked but partially... Now the activation works without issue, but the problem is that at times the focus is taken by my application and it doesnt deactivate on repeated mouse click. This condition is solved only if I click my application and then click on other application. I saw the similar question asked here, http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/546858a1-5122-478a-944d-8c6e8383b905/ but I am unable to understand the solution that he has provided in the end. can someone please help. – vikramaditya234 Feb 10 '12 at 12:04