Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have written a small form application. 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. LControlKey+LControlKey for the first time the application get proper control.
But when I deactivate and redo the shortcut key, the icon of the application on the taskbar starts blinking and textbox in the application seems to get the focus(cursor blinking) but the control is still not reached. I say this because when i try to write something nothing gets written on the application.

I am giving focus to the application by using form.Activate() I have also tried from.Visible but with no luck.
Does any have any idea why is this happening?
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jan-12 22:51pm    
What is "LControlKey+LControlKey". You probably mistyped it.
--SA
Sergey Alexandrovich Kryukov 27-Jan-12 23:12pm    
Did you managed to activate form, forget about the focus? You can tell if it is activated or not just by the color of the title bar.
--SA
vikramaditya234 28-Jan-12 5:10am    
"LControlKey+LControlKey" means "left control key" pressed twice.
The color of the title bar is not changing, however the event of form activated is getting called and the icon on the taskbar is blinking.
Here is how I am doing it:
1. I have one form class and another operation class. The object of the form class has been passed to the operation class.
2. The logic for implementation of shortcut key is in operation class and when the shortcut key is detected the object of form is used to activate the form.
3. Once form1.activate() is processed the event for form activated is triggered and the respective function is called from inside form class.
4. I have tried using form1.focus() from operation class and in form class both with no luck.
5. I even disabled all the controls present inside the form, but this also didnt help.

Trying all the above still gives the same output. That is, when the form is deactivated or minimized and the shortcut key is used for the 1st time, the form gets activated, with the change in the title bar. Now I take the form out of focus(deactivate or minimize) and again use shortcut key to activate the form, this time the form is not activated and the icon in the taskbar starts blinking.
The proper activation works only once, not after that.

1 solution

What you say sounds a bit weird, but yes, one can screw up the focusing behavior doing certain things. Unfortunately, you did not describe you behavior in full. I understand, this is not too easy.

Here is what you can do: you should create a shorted possible code sample manifesting the problem. If better be in one file (you can do it by avoiding using Designer and writing everything in code in the same file as your entry point (Main method). Two short files would be acceptable, too, but make it short but complete. Describe in detail the steps; what to click and hit. Post it using "Improve question" above. Chances are, you will fix the problem by yourself, because just the fact you prepare a short code sample focusing only on one problem often helps to get things. In this case, don't post anything except your comment "it works now". OK?

In the meanwhile, let me explain it the essence of the focusing. The "focus" means "keyboard focus" and nothing else. At any given moment of time, there is only on focus on the whole system. You can focus a control calling System.Windows.Forms.Control.Focus. There is not such operation as "defocus" as it would be illogical and contradictory to the previous statement. Therefore, the control can only loos focus if something else is focused.

Now, for simplicity, let's consider only the forms which have at least one enabled control at all times. The method Form.Activate does not directly focus anything, it activates the form. If the form was deactivated with some control focused, this "last focused" control is remembered by the form. If the form is activated again, the focus is remembered; and the appropriate control is automatically focused upon activation.

In principle, you could create pathological situation before the form was deactivated; for example, disable a focused control, hide it or delete it. It could be something else. Where is the focus in this case — I don't know, it depends on the number of factors. Honestly, I cannot imagine all possible situations; I admit the situation with focus could be complex enough, so I would not be able to fix it without a code sample. Start with trying by yourself.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900