我是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;
}
}以下代码不起作用。
发布于 2021-03-01 22:34:39
所以你想这么做
表单中的任何位置。
执行此操作的最佳方法是重写
方法。
但它也有一些局限性。您可以使用
密钥只有一个以上的密钥。
例如:
作品
作品
作品
作品
不工作
不工作
因此您必须使用另一个密钥,例如使用
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);
}发布于 2021-03-01 21:43:15
基本上,您希望跟踪
形式。
首先
,您可以
基表中的所有表单(表单的
属性应为
),就像;
public class BaseForm : Form
{
private static List keyPress = new List(); // 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事件。
第二
,您可以使用全局键挂钩。(更多信息请点击
这个
)在主线程入口处
将被附加到进程中,并且每个关键事件都将在
..。
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 keyPress = new List();
///
/// The main entry point for the application.
///
[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);
}
}如果有多个表单。表单控件应该可以从全局访问,或者表单应该具有自定义方法,如
..。
发布于 2021-03-01 19:27:01
您可能会错过'&&‘
您还可以尝试此Javascript代码:
document.addEventListener('keydown', logKey);
function logKey(e) {
log.textContent += ` ${e.code}`;
}https://stackoverflow.com/questions/66420956
复制相似问题