0

Possible Duplicate:
Best way to implement keyboard shortcuts in winforms?

I have a probleme with KeyDown or KeyPress or KeyUp. I creat a new KeyDown for the form (private void Form1_KeyDown(object sender, KeyEventArgs e)) where I insert this code:

if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control)
                MessageBox.Show("yes");

If you pess Ctrl + A will show you a MessageBox with Yes message.

The probleme is this: if I create a new project (Windows form application) will work perfectly, but if I add the code in my Windows form application (have like 4201 code lines) will not work, and I don't know what is the issues. I don't know what are the issues in this case.

Maybe is becouse the librarys:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Media;
using System.Diagnostics;
using Microsoft.Win32;
using System.Net.Mail;
using System.Net;

Or the code that I add in Program.cs for let the people start only once time the app:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace Programari
{
    static class Program
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            bool createdNew = true;
            using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
            {
                if (createdNew)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
                }
                else
                {
                    Process current = Process.GetCurrentProcess();

                    if(Settings1.Default.rosaueng == 0)
                        MessageBox.Show("smartAppointment este deja deschis, ii gasesti iconita in System Icons !","EROARE DESCHIDERE APLICATIE");
                    else
                        MessageBox.Show("smartAppointment is already open, you can find him at System Icons !", "ERROR OPEN APPLICATION");

                    foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                    {
                        if (process.Id != current.Id)
                        {
                            SetForegroundWindow(process.MainWindowHandle);
                            break;
                        }
                    }
                }
            }
        }
    }
}

If you know, please tell me. Thanks !

Community
  • 1
  • 1
AnDr3yy
  • 239
  • 2
  • 5
  • 16
  • Only the window with the focus gets the KeyDown event. Which will never be form if it has any focusable controls. Override ProcessCmdKey instead. – Hans Passant Sep 05 '12 at 18:48
  • why not just have keypreview = true and then handle keydown? – nawfal Sep 05 '12 at 18:51
  • This is not a duplicate--it just suffers from poor wording. Seems like a copy/paste problem and an event handler that is too vague to be useful for key detection. – Brian Warshaw Sep 05 '12 at 19:05

2 Answers2

0

If your code is exactly as you have it written above, the problem is that you are typing the event args in your handler to EventArgs instead of the more specific KeyEventArgs. EventArgs does not have a KeyCode property. Again, if what you pasted above is what you have, your issue is likely that you can't compile, and this is the reason why.

UPDATE

Here's what the proper method signature would look like for handling KeyDown:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
  // Your code here
}
Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72
  • Sorry, I copied badly, was `private void Form1_KeyDown(object sender, EventArgs e)` . Why can't compile, the app work perfecly in rest ! – AnDr3yy Sep 05 '12 at 18:55
  • If you can't compile, the problem is exactly as I have said. You need to use a `KeyEventHandler` and your method signature should read `private void Form1_KeyDown(object sender, KeyEventArgs e)`. – Brian Warshaw Sep 05 '12 at 18:58
  • In this case I try, but don't work ! I resolve the problem with the KeyDown, but how I can make that if I press Ctr+N, will work even if the app is close in System Icons ? – AnDr3yy Sep 05 '12 at 19:09
0

I try the method that Hans Passant told me and work perfectly.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.N))
            {
                //button5_Click(this, new EventArgs());
                MessageBox.Show("da");
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

But I try to run the button5 when I will press Ctrl+N and the code button5_Click(this, new EventArgs());, don't work. How I can call the button5_Click function ?

AnDr3yy
  • 239
  • 2
  • 5
  • 16
  • `button5_Click(this, null);` will work perfectly, sorry for the question, but now I have another question. I user Form_Closing, after I press X, whe app will go in System Icons, but how I can do that if i press Ctrl+N to call the function if the app is in System Icons ? – AnDr3yy Sep 05 '12 at 19:07
  • Hey, I'm glad you got past your initial hurdle. But to keep things clean and organized, you should probably open separate questions for your other inquiries. Keep things easier to understand. – Brian Warshaw Sep 05 '12 at 19:11