Click here to Skip to main content
15,936,850 members
Please Sign up or sign in to vote.
2.22/5 (2 votes)
I created one windows form keypress application.And I am using F3,F4,F5 keypress for some operation. But keypress is not working while form is minimised. It should work when form is minimised.How to fix this problem in C#.

C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
       {
               this.WindowState = FormWindowState.Minimized;

               oWord.Visible = true;
               if (keyData == Keys.F3)
               {
                   this.WindowState = FormWindowState.Minimized;
                   CaptureWithTaskBar();
                   this.WindowState = FormWindowState.Normal;
               }
               else if (keyData == Keys.F4)
               {
                   this.WindowState = FormWindowState.Minimized;
                   CaptureWithoutTaskBar();
                   this.WindowState = FormWindowState.Normal;
               }
               else if (keyData == Keys.F5)
               {
                   SaveDocument();
               }
}
Posted
Updated 21-Jan-21 5:44am
v4

1 solution

The problem is that when the form is minimised, it doesn't have the focus - so it doesn;t get any keyboard activity.

If you want to catch keystrokes when minimized - and I'd advise against it, it annoys users if the keys are used by other apps like F3, F4, and F5 often are - then you have to start doing very complex code to install a Global Hook: Google[^] - but do not expect it to be easy, and do expect your attempts to crash your PC or leave it in a very unstable state a couple of times while you are developing it.
 
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