时间:2019-05-10 标签:c#specific sequence keypress event anywhere in form

Posted

技术标签:

【中文标题】时间:2019-05-10 标签:c#specific sequence keypress event anywhere in form【英文标题】:c# specific sequence keypress event any where in form 【发布时间】:2021-05-30 21:48:03 【问题描述】:

我是 C# 的初学者,想要一些关于如何解决以下问题的建议:

随机按以下键“ctrl + v + a”表单中的任何位置时,我想创建一个事件以启用某些文本框。

  private void test_KeyDown(object sender, KeyEventArgs e)
    
        if ((int)e.KeyData == (int)Keys.Control + (int)Keys.V + (int)Keys.A)
        
            textbox1.Enabled = true;
            textbox2.Enabled = true;

        
    

以下代码不起作用。

【问题讨论】:

WPF/WinForms/UWP项目吗? @ekvalizer 是 WinForms 项目。 随机按是什么意思?如果我只按 v. 是否足以启用? 无论我在表单中做什么,当我按下特定的键序列时,我都希望自动启用一些文本框。我将使用它来校准某些系统,它需要使用特定的密钥序列进行保护。 @Berkay 就像,你不能在文本框中放任何东西(它被禁用),直到你通过按特定的键序列(Ctlr+v+a 或任何其他设置的序列)解锁文本框跨度> 【参考方案1】:

基本上你想跟踪key events的表单。

首先,你可以inherit你所有的表单来自基本表单(表单的KeyPreview属性应该是True),比如;

public class BaseForm : Form

    private static List<Keys> keyPress = new List<Keys>(); // to store the key values
    public BaseForm()
    
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.BaseForm_KeyDown); // register event for all forms which inherits from BaseForm
    

    private void BaseForm_KeyDown(object sender, KeyEventArgs e)
    
        if (e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.C || e.KeyCode == Keys.V) // right now Control, C, V keys are included
        

            if (keyPress.Any(x => x == e.KeyCode)) // If keypress list has the current key press so pattern is broken
            
                keyPress.Clear(); //clear the list user should start again
            
            else
            
                keyPress.Add(e.KeyCode);
            

            if (keyPress.Count > 2) 
            
                this.Controls["textBox1"].Enabled = true; 
                keyPress.Clear(); // clear the keyPress list
            
        
    

因此,通过这样做,您无需为所有表单注册 keydown 事件。

第二,可以使用全局key hook。 (更多信息请点击this)在主线程入口处Low-LevelKeyboardHook将附加到进程中,每个关键事件都将在HookCallback处捕获。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Linq;

namespace KeyHookTryApp

   static class Program
   
       private const int WH_KEYBOARD_LL = 13;
       private const int WM_KEYDOWN = 0x0100;
       private static LowLevelKeyboardProc _proc = HookCallback;
       private static IntPtr _hookID = IntPtr.Zero;
       private static Form1 f;
       private static List<Keys> keyPress = new List<Keys>();
       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       [STAThread]
       static void Main()
       
           _hookID = SetHook(_proc);
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
           f = new Form1();
           Application.Run(f);
           UnhookWindowsHookEx(_hookID);
       
       private static IntPtr SetHook(LowLevelKeyboardProc proc)
       
           using (Process curProcess = Process.GetCurrentProcess())
           using (ProcessModule curModule = curProcess.MainModule)
           
               return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                   GetModuleHandle(curModule.ModuleName), 0);
           
       

       private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

       private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
       
           int vkCode = Marshal.ReadInt32(lParam);
           Keys key = (Keys)vkCode;

           if (nCode >= 0 && ((key == Keys.LControlKey || key == Keys.RControlKey) || key == Keys.C || key == Keys.V))
           
               if (keyPress.Any(x => x == key))
               
                   keyPress.Clear();
               
               else
               
                   keyPress.Add(key);
                

               if (keyPress.Count > 2)
               
                   f.Controls["textBox1"].Enabled = true;
                   keyPress.Clear();
               
           
           else if(keyPress.Count > 0)
           
               keyPress.Clear();
           

           return CallNextHookEx(_hookID, nCode, wParam, lParam);
       

       [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
       private static extern IntPtr SetWindowsHookEx(int idHook,LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

       [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
       [return: MarshalAs(UnmanagedType.Bool)]
       private static extern bool UnhookWindowsHookEx(IntPtr hhk);

       [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
       private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,IntPtr wParam, IntPtr lParam);

       [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
       private static extern IntPtr GetModuleHandle(string lpModuleName);
   

如果有多种形式。表单控件应该可以从全局访问,或者表单应该有自定义方法,如EnableTextBoxes()

【讨论】:

【参考方案2】:

所以你想这样做任何地方在表单中。 最好的方法是覆盖 Form 的 ProcessCmdKey 方法。 但它有一些限制。您只能将Control | Alt | Shift 键与另外一个键一起使用。

赞:ctrl + V 有效 ctrl + A 有效 ctrl + alt + V 有效 ctrl + alt + shift + V 有效 ctrl + V + A 无效@987654328 @ 不起作用

所以你必须使用另一个键,例如使用ctrl + shift + V

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    
        switch (keyData)
        
            case (Keys.Control | Keys.Shift | Keys.V):
                textbox1.Enabled = true;
                textbox2.Enabled = true;
                return true;

      // more keys if you want

            case (Keys.Control | Keys.H):
                MessageBox.Show("Hello World!");
                return true;
        

        return base.ProcessCmdKey(ref msg, keyData);
    

【讨论】:

【参考方案3】:

你可能会错过'&&' if ((int)e.KeyData == (int)Keys.Control &amp;&amp; (int)e.KeyData == (int)Keys.V &amp;&amp; (int)e.KeyData == (int)Keys.A))

你也可以试试这个 javascript 代码:

document.addEventListener('keydown', logKey);

function logKey(e) 
  log.textContent += ` $e.code`;

【讨论】:

【参考方案4】:

if条件下可以给e.Control==true && e.KeyCode==Keys.V && e.Keycode==Keys.A

【讨论】:

【参考方案5】:

因为KeyEventArgs e 只存储一个密钥,我假设您必须存储密钥V 状态。

private bool _keyVPressed;

private void Form1_KeyDown(object sender, KeyEventArgs e)

    _keyVPressed = _keyVPressed || KeyVPressed(e.KeyCode);
    if (e.Control && _keyVPressed && e.KeyCode == Keys.A)
    
        textBox1.Enabled = true;
    


private void Form1_KeyUp(object sender, KeyEventArgs e) => _keyVPressed = !KeyVPressed(e.KeyCode);

private bool KeyVPressed(Keys k) => k is Keys.V;

要在表单中的任何位置应用此行为,您需要将事件附加到表单中的每个控件

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);
//...
//some designer code
//...
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyUp);

【讨论】:

【参考方案6】:

一种方法是处理KeyDown 事件,如果第一个键(例如A 已关闭且Ctrl 键)在Form 处将bool 设置为true level 表示序列正在开始。下一个KeyDown 事件应该是序列中的第二个键,比如V,并且Ctrl 键按下(仍然)。如果你在开始时设置的bool 仍然为真,这意味着Ctrl 键还没有被释放,那么执行你想做的任何事情。使用KeyUp 事件检测Ctrl 键是否已被释放,如果已释放,请将bool 值设置为false,因为它不是按顺序排列的。

伪代码如下所示:

private bool isSequence; KeyDown 事件处理程序 if (Ctrl &amp;&amp; e.KeyData == Keys.A) isSequence = true; if (Ctrl &amp;&amp; e.KeyData == Keys.V &amp;&amp; isSequence) DoWhateverThing(); KeyUp 事件处理程序 if (e.KeyData == Ctrl) isSequence = false;

【讨论】:

以上是关于时间:2019-05-10 标签:c#specific sequence keypress event anywhere in form的主要内容,如果未能解决你的问题,请参考以下文章

时间:2019-05-10 标签:c#release optimizationunusedstring

时间:2019-05-10 标签:c#socketconnectionwithmultithreadingtelnet

时间:2019-05-10 标签:c#paypalrestapitransactionsearch

时间:2019-05-10 标签:c#socketThread

时间:2019-05-10 标签:c++threadeddbclassmemorymixup

时间:2019-05-10 标签:c#applicationautostartwindows7