如何在c# winforms中读取键盘上的键Form3 panel1_Paint button10()

c6ubokkw  于 2023-03-31  发布在  C#
关注(0)|答案(1)|浏览(148)

我有一个panel1_Paint用于在panel2_Paint中绘制显示。我正在像这样从form_KeyPress阅读键盘。

private void form_KeyPress(object sender, KeyPressEventArgs e)
    {
        scankeyinput3 += e.KeyChar.ToString();

        if (scankeyinput3 != "") { scankeyinput4 = ""; }

        if (keyboardInputLength2 >= 1)
        {
            scankeyinput4 = scankeyinput3.Substring(scankeyinput3.Length - 1);
        }
        scankeyinput3 = scankeyinput4;

        ProcessCmdKey(ref msg, keydata);
    }

我正在使用ProcessCmdKey获取Form3中的键盘键。

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
    {
        scankeyinput6 = keyData.ToString();
        Form3.textchangedtextbox2 = 4;

        if (scankeyinput6 == "None") { scankeyinput6 = "@"; }
        if (scankeyinput6 == "Next") { scankeyinput6 = "@"; }

        if (scankeyinput6 == "@") { Form3.textchangedtextbox2 = 0; }
        if (scankeyinput6 == "#") { Form3.textchangedtextbox2 = 0; }

        if (scankeyinput6 != "") { scankeyinput4 = ""; }
        keyboardInputLength2 = scankeyinput6.Length;

        if (keyboardInputLength2 >= 1)
        {
            scankeyinput4 = scankeyinput6.Substring(scankeyinput6.Length - 1);
        }
        scankeyinput6 = scankeyinput4;

        return base.ProcessCmdKey(ref msg, keyData);
    }

我遇到的问题是当我在Form3 panel1_Paint button10()中时获取键盘键。我无法在Form3 panel1_Paint button10()上获得焦点。因此,我无法从保护覆盖bool ProcessCmdKey(ref System.Windows.Forms.Message msg,System.Windows.Forms.Keys keyData)中读取键
我想知道如何在button10()上获得焦点。我用这里的代码读取键盘以返回form,Form1,Form2和From3中的键。我无法在button10()上获得焦点。
我尝试使用文本框获取密钥,但没有功能。

iyfamqjs

iyfamqjs1#

从您的描述来看,您似乎希望接收“热键”类型的消息,而不管哪个窗体或控件处于焦点状态。您可以通过为主窗体实现IMessageFilter来实现此目标。

public partial class MainForm : Form, IMessageFilter
{
    public MainForm()
    {
        InitializeComponent();
        Application.AddMessageFilter(this);
        Disposed += (sender, e) =>Application.RemoveMessageFilter(this);
    }

    const int WM_KEYDOWN = 0x0100;
    public bool PreFilterMessage(ref Message m)
    {
        if(m.Msg.Equals(WM_KEYDOWN))
        {
            Keys key = (Keys)m.WParam | ModifierKeys;
            ProcessKeyboardHookKey(new KeyEventArgs(key));
        }
        return false;
    }

    private void ProcessKeyboardHookKey(KeyEventArgs e)
    {
        string scankeyinput6 = e.KeyData.ToString();
        Debug.WriteLine(scankeyinput6);

    #if false
        // D O    S O M E T H I N G

        Form3.textchangedtextbox2 = 4;

        if (scankeyinput6 == "None") { scankeyinput6 = "@"; }
        if (scankeyinput6 == "Next") { scankeyinput6 = "@"; }

        if (scankeyinput6 == "@") { Form3.textchangedtextbox2 = 0; }
        if (scankeyinput6 == "#") { Form3.textchangedtextbox2 = 0; }

        if (scankeyinput6 != "") { scankeyinput4 = ""; }
        keyboardInputLength2 = scankeyinput6.Length;

        if (keyboardInputLength2 >= 1)
        {
            scankeyinput4 = scankeyinput6.Substring(scankeyinput6.Length - 1);
        }
        scankeyinput6 = scankeyinput4;
    #endif
    }
}

相关问题