如题。因为我有些操作不是控件事件KeyPress可以判断得到的。就好像我在某控件下的
KeyPress(object ...., KeyPressEventArg e)
{
if (e.KeyChar == '\r')//ENTER键,回车键:确定操作
{
MessageBox.Show("OK");//有反应
}
if (e.KeyChar == Convert.ToChar(27))//ESC键:取消操作
{
MessageBox.Show("Cancle");//无反应,所以我只想到用Windows的ESC键下按的消息来做处理好了。但怎么写呢。
}
}
KeyPress(object ...., KeyPressEventArg e)
{
if (e.KeyChar == '\r')//ENTER键,回车键:确定操作
{
MessageBox.Show("OK");//有反应
}
if (e.KeyChar == Convert.ToChar(27))//ESC键:取消操作
{
MessageBox.Show("Cancle");//无反应,所以我只想到用Windows的ESC键下按的消息来做处理好了。但怎么写呢。
}
}
解决方案 »
- WCF客户端App.Config配置问题
- 我快被气晕了,combobox总是返回“System.Data.DataRowView"值???
- 如何想Excel中插入一行
- Cookie 中不能保存换行符?
- 简单问题:如果知道DataTable某行某列对应的值,如何取得该行其它列对应的值??
- 编写一个C#程序,急用!!!编写的用序号代替题号,谢谢~!
- 怎么连标题一起导入到Excel
- c# webbrowers中怎样给textarea 填写文字?
- 怎样判断一个控件是否为一个容器控件?
- 这个数据库怎么搭建
- 用C#的Net.Mail发的电子邮件,到接收人邮件里总是发现个别字或连续几个字出现乱码,是怎么回事
- 请教一个正则表达式?
你用这个
if (e.KeyValue == 27)
I had try it before you case... Enter Key will tigger Both KeyDown and KeyPress the Event...
But , ESC Key would not tigger these two events... You can try it ... before you will leave this message...
KeyDown KeyPress都试过,就Enter键是可以的。ESC的按下,都不会触发这两个事件。
{
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto)]
public static extern bool UnhookWindowsHookEx(int idHook); public delegate int HookProc(int nCode, int wParam, IntPtr lParam); // Fields
private static int hKeyboardHook;
private HookProc KeyboardHookProcedure;
public const int WH_KEYBOARD_LL = 13; // Methods
static KeyHook()
{
hKeyboardHook = 0;
} private int code = 0; public KeyHook()
{
this.Start(); } public KeyHook(int keyCode)
{
this.Start();
this.code = keyCode;
} private int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
{
KeyboardHookStruct struct2 = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
KeyEventArgs args = new KeyEventArgs((Keys)struct2.vkCode);
Console.WriteLine(args.KeyValue.ToString());
//屏蔽
//91,92 ==> win键
//93 ==> app键
// 9 ==> tab 键
//18 ==> alt 键
if (args.KeyValue == code || args.KeyValue == 9 || args.KeyValue == 91 || args.KeyValue == 92 || args.KeyValue == 93)
{
return 1;
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
public void Hook()
{
this.Start();
}
public void UnHook()
{
this.Stop();
}
public void Start()
{
if (hKeyboardHook == 0)
{
this.KeyboardHookProcedure = new HookProc(this.KeyboardHookProc);
Module m = Assembly.GetExecutingAssembly().GetModules()[0];
IntPtr p = Marshal.GetHINSTANCE(m);
hKeyboardHook = SetWindowsHookEx(13, this.KeyboardHookProcedure, p, 0);
if (hKeyboardHook == 0)
{
this.Stop();
Log.WriteErLog("KeyHook:SetWindowsHookEx ist failed.");
//throw new Exception("SetWindowsHookEx ist failed.");
}
}
} public void Stop()
{
bool flag = true;
if (hKeyboardHook != 0)
{
flag = UnhookWindowsHookEx(hKeyboardHook);
hKeyboardHook = 0;
}
if (!flag)
{
Log.WriteErLog("KeyHook:SetWindowsHookEx ist failed.");
//throw new Exception("UnhookWindowsHookEx failed.");
}
}
~KeyHook()
{
this.Stop();
}
[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo; public KeyboardHookStruct()
{
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
//do something;
}
}
keys是个枚举类型,里面包含了所有的键值。
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter )
{
}
if (e.KeyCode == Keys.Escape)
{
} } private void Form2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar ==(char )Keys.Enter )
{
} if (e.KeyChar ==(char )Keys .Escape )
{
} }
这样应该可以了吧