获取当前方法事件对应的控件
例如,button1对应的事件button1_Click():
private void button1_Click(object sender, EventArgs e)
{
…
}
我们想在方法内部获得事件方法对应的控件相关参数:名称、内容、数据…
可以通过事件中传递过来的sender对象获取名称
因为我们的控件是button类。所以用—>(Button) sender,
同理,其他类型的控件就是就是—>(控件类) sender
…
((ComboBox) sender).Name//名称
((ComboBox) sender).Text//获取文本
监听按键-创建类来使用
c#按键键盘事件监控键盘上下左右和其它按键
重写:protected override bool ProcessDialogKey(Keys keyData)就可以实现了。
放在窗口类里面
public partial class Form_XYZ_Jog : Form
{
xxxxx
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| protected override bool ProcessDialogKey(Keys keyData) { string direction2 = keyData.ToString(); string Left = "A"; string Right = "D"; string Front = "W"; string Back = "S"; string Up = "Up"; string Down = "Down"; label_press_msg.Text = "已按下:" + direction2; Console.WriteLine(DateTime.Now.ToString()+ label_press_msg.Text); if (direction2 ==Left) { button_move_left.Focus(); EnableLeftRightFrontBack(true); EnableUpDown(false); } else if(direction2 == Right) { button_move_right.Focus(); EnableLeftRightFrontBack(true); EnableUpDown(false); } else if (direction2 == Front) { button_move_front.Focus(); EnableLeftRightFrontBack(true); EnableUpDown(false); } else if (direction2 == Back) { button_move_back.Focus(); EnableLeftRightFrontBack(true); EnableUpDown(false); } else if (direction2 == Up) { button_move_down.Focus(); EnableLeftRightFrontBack(false); EnableUpDown(true); } else if (direction2 == Down) { button_move_up.Focus(); EnableLeftRightFrontBack(false); EnableUpDown(true); } return base.ProcessDialogKey(keyData); }
|
全局监听
C#如何获取键盘和鼠标处于空闲状态下的时间 可以通过windows api 函数 GetLastInputInfo或者全局钩子HOOK来实现
用 GetLastInputInfo 写(键鼠空闲15分钟自动弹出一个页面)
新建windows 应用程序项目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| using System; using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices; namespace APPDEMO { static class Program { private static VedioForm vf = null; private static System.Windows.Forms.Timer timer1 = null; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (timer1 == null) { timer1 = new Timer(); } timer1.Interval = 60 * 1000; timer1.Tick += new EventHandler(timer1_Tick); timer1.Start(); Application.Run(new MainForm()); } private static void timer1_Tick(object sender, EventArgs e) { if (GetIdleTick() / 1000 >= 15 * 60) { ShowVidioForm(); } } private static void ShowVidioForm() { try { if (vf == null) { vf = new VedioForm(); } vf.ShowDialog(); } catch { } } public static long GetIdleTick() { LASTINPUTINFO lastInputInfo = new LASTINPUTINFO(); lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo); if (!GetLastInputInfo(ref lastInputInfo)) return 0; return Environment.TickCount - (long)lastInputInfo.dwTime; } [StructLayout(LayoutKind.Sequential)] private struct LASTINPUTINFO { [MarshalAs(UnmanagedType.U4)] public int cbSize; [MarshalAs(UnmanagedType.U4)] public uint dwTime; } [DllImport("user32.dll")] private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); } }
|
用HOOK钩子来实现 创建钩子类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TengDa.Core { class KeyboardHook { public event KeyEventHandler KeyDownEvent; public event KeyPressEventHandler KeyPressEvent; public event KeyEventHandler KeyUpEvent; public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam); static int hKeyboardHook = 0; public const int WH_KEYBOARD_LL = 13; HookProc KeyboardHookProcedure; [StructLayout(LayoutKind.Sequential)] public class KeyboardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern bool UnhookWindowsHookEx(int idHook); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam); [DllImport("kernel32.dll")] static extern int GetCurrentThreadId(); [DllImport("kernel32.dll")] public static extern IntPtr GetModuleHandle(string name); public void Start() { if (hKeyboardHook == 0) { KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0); SetWindowsHookEx(13, KeyboardHookProcedure, IntPtr.Zero, GetCurrentThreadId()); if (hKeyboardHook == 0) { Stop(); throw new Exception("安装键盘钩子失败"); } } } public void Stop() { bool retKeyboard = true; if (hKeyboardHook != 0) { retKeyboard = UnhookWindowsHookEx(hKeyboardHook); hKeyboardHook = 0; } if (!(retKeyboard)) throw new Exception("卸载钩子失败!"); } [DllImport("user32")] public static extern int ToAscii(int uVirtKey, //[in] 指定虚拟关键代码进行翻译。 int uScanCode, // [in] 指定的硬件扫描码的关键须翻译成英文。高阶位的这个值设定的关键,如果是(不压) byte[] lpbKeyState, // [in] 指针,以256字节数组,包含当前键盘的状态。每个元素(字节)的数组包含状态的一个关键。如果高阶位的字节是一套,关键是下跌(按下)。在低比特,如果设置表明,关键是对切换。在此功能,只有肘位的CAPS LOCK键是相关的。在切换状态的NUM个锁和滚动锁定键被忽略。 byte[] lpwTransKey, // [out] 指针的缓冲区收到翻译字符或字符。 int fuState); [DllImport("user32")] public static extern int GetKeyboardState(byte[] pbKeyState); [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern short GetKeyState(int vKey); private const int WM_KEYDOWN = 0x100; private const int WM_KEYUP = 0x101; private const int WM_SYSKEYDOWN = 0x104; private const int WM_SYSKEYUP = 0x105; private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { if ((nCode >= 0) && (KeyDownEvent != null || KeyUpEvent != null || KeyPressEvent != null)) { KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs(keyData); KeyDownEvent(this, e); } if (KeyPressEvent != null && wParam == WM_KEYDOWN) { byte[] keyState = new byte[256]; GetKeyboardState(keyState); byte[] inBuffer = new byte[2]; if (ToAscii(MyKeyboardHookStruct.vkCode, MyKeyboardHookStruct.scanCode, keyState, inBuffer, MyKeyboardHookStruct.flags) == 1) { KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]); KeyPressEvent(this, e); } } if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { Keys keyData = (Keys)MyKeyboardHookStruct.vkCode; KeyEventArgs e = new KeyEventArgs(keyData); KeyUpEvent(this, e); } } return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam); } ~KeyboardHook() { Stop(); } } }
|
调用 方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| private void hook_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode.Equals(Keys.PrintScreen)) {
} }
public void startListen() { myKeyEventHandeler = new KeyEventHandler(hook_KeyDown); k_hook.KeyDownEvent += myKeyEventHandeler; k_hook.Start(); }
public void stopListen() { if (myKeyEventHandeler != null) { k_hook.KeyDownEvent -= myKeyEventHandeler; myKeyEventHandeler = null; k_hook.Stop(); } }
|
相关链接(侵删)
- c# 获取当前方法事件对应的控件
- c#按键键盘事件监控上下左右和其它按键
- C#全局监听键盘事件
=================我是分割线=================
欢迎到公众号来唠嗑: