KeyDown的问题,不起作用

时间:2012-09-05 18:45:17

标签: c# keypress keydown keyup

  

可能重复:
  Best way to implement keyboard shortcuts in winforms?

我对KeyDown或KeyPress或KeyUp有疑问。我为表单(private void Form1_KeyDown(object sender, KeyEventArgs e))创建了一个新的KeyDown,我插入了这段代码:

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

如果您输入Ctrl + A将显示带有Yes消息的MessageBox。

问题是这样的:如果我创建一个新项目(Windows form application)将完美地工作,但如果我在我的Windows窗体应用程序中添加代码(有类似4201代码行)将无法正常工作,我不会不知道是什么问题。我不知道在这种情况下会出现什么问题。

也许是因为图书馆:

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;

或者我在Program.cs中添加的代码,让人们只启动应用程序一次:

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;
                        }
                    }
                }
            }
        }
    }
}

如果你知道,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:0)

如果您的代码完全按照上面的说明编写,则问题是您在处理程序中将事件args键入EventArgs而不是更具体的KeyEventArgsEventArgs没有KeyCode属性。同样,如果您上面粘贴的是您所拥有的,那么您的问题可能是您无法编译,这就是原因。

<强>更新

以下是处理KeyDown的正确方法签名:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
  // Your code here
}

答案 1 :(得分:0)

我尝试了Hans Passant告诉我的方法并且完美地工作。

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);
        }

但是当我按Ctrl + N并且代码button5_Click(this, new EventArgs());时,我尝试运行button5,不起作用。我如何调用button5_Click函数?