如何区分两个回车键

ljlsucfe 2009-07-22 12:29:07
大家好,在开发过程中遇到一个问题,向大家请教!
要达到的目的是:区分键盘中的两个回车键,我采用的方法是通过键盘挂钩来处理,代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

namespace HookTest
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
//委托
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
static int hHook = 0;
public const int WH_KEYBOARD_LL = 2;

HookProc KeyBoardHookProcedure;
//键盘Hook结构函数
[StructLayout(LayoutKind.Sequential ) ]
public class KeyBoardHookStruct
{

public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}

#region DllImport
//设置钩子
[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, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32")]
public static extern int GetCurrentThreadId();
#endregion
#region 自定义事件
public void Hook_Start()
{
// 安装键盘钩子
if (hHook == 0)
{
KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL,
KeyBoardHookProcedure,
IntPtr.Zero, GetCurrentThreadId());
//如果设置钩子失败.
if (hHook == 0)
{
Hook_Clear();
throw new Exception("设置Hook失败!");
}
}
}

//取消钩子事件
public void Hook_Clear()
{
bool retKeyboard = true;
if (hHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hHook);
hHook = 0;
}
//如果去掉钩子失败.
if (!retKeyboard) throw new Exception("UnhookWindowsHookEx failed.");
}

//这里可以添加自己想要的信息处理
public int KeyBoardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if ((nCode >= 0)&&((int)wParam ==13))
{
KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
//MessageBox.Show(kbh.vkCode.ToString() + " " + kbh.flags.ToString());
//MessageBox.Show(nCode.ToString() + "-" + wParam.ToString() + "-" + lParam.ToString());

}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
#endregion



public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//

}

void Start_BtnClick(object sender, EventArgs e)
{
this.Hook_Start();
}

void Clear_BtnClick(object sender, EventArgs e)
{
this.Hook_Clear();
}
}
}



问题出在这里:

//这里可以添加自己想要的信息处理
public int KeyBoardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if ((nCode >= 0)&&((int)wParam ==13))
{
KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));
//MessageBox.Show(kbh.vkCode.ToString() + " " + kbh.flags.ToString());
//MessageBox.Show(nCode.ToString() + "-" + wParam.ToString() + "-" + lParam.ToString());

}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}

我想将lParam转换成KeyBoardHookStruct 来检查两个回车键的详细信息有什么不同,这里提示错误:尝试读取或写入受保护的内存,这通常指示其它内存已损坏。
向大家请教一下,怎么解决。另外,两个回车键得到的kbh,到底是哪里不一样?
...全文
370 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljlsucfe 2009-07-22
  • 打赏
  • 举报
回复
经测试, 从右到左第24位,是1就是扩展键盘或是小键盘。
gaijf 2009-07-22
  • 打赏
  • 举报
回复
ding
ljlsucfe 2009-07-22
  • 打赏
  • 举报
回复
lParam与WM_KEYDOWN同,占四个字节,其包括的内容较多,其二进制结构如下:

0
1
……
15
16
………
23
24
25
……
28
29
30
31
  0-15位(Key repeat count),键码重复次数。16-23位(Scan code),按键的扫描码。24位(Extended_Key flag),扩展键(功能键、数字小键盘上的键)标志,为1则是扩展键,否则为0。25-28位被保留。29位(Context Code),状态描述码,ALT键被按下则为1,否则为0。30位(Previouskey_stateflag)指定先前的键状态,如果消息被发出之前键处于按下状态,则为1;键处于释放状态则为0。31位(Transiton_stateflag)状态转换标志,如果键是被按下值为1,如果键被放开值为0。
如果这样的话,不用转,直接判断lparam的第24位应该就行,试下先!

wenblue7 2009-07-22
  • 打赏
  • 举报
回复
顶起
ljlsucfe 2009-07-22
  • 打赏
  • 举报
回复
现在的问题是用勾子拦截了,得不到扫描码,转换过程中出错了
ff1222 2009-07-22
  • 打赏
  • 举报
回复
有区别,它们的键盘扫描码是不同的,用键盘钩子拦截才能看到,普通的KeyPress事件获取不到~~

百度知道里有人问过
ljlsucfe 2009-07-22
  • 打赏
  • 举报
回复
zengfanxing:他们的值都是13,区分不了啊
红街咖啡 2009-07-22
  • 打赏
  • 举报
回复
定一下。我都是用。他们的值来区分。
ljlsucfe 2009-07-22
  • 打赏
  • 举报
回复
大家帮忙顶一下啊!
  • 打赏
  • 举报
回复
mark
yeaicc 2009-07-22
  • 打赏
  • 举报
回复




Update

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧