Unity键盘钩子

26 篇文章 1 订阅
15 篇文章 0 订阅

http://blog.csdn.net/qq452626100/article/details/52398830

private static int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
	if (nCode == HC_ACTION	)
	{
		var kc = (KeyCode)(wParam+97-65);

		// https://msdn.microsoft.com/en-us/library/windows/desktop/ms644984(v=vs.85).aspx
		int int32Param = lParam.ToInt32();
		var repeat_count	= (ushort) (int32Param&		0xFFFF); //0 - 15	The repeat count.The value is the number of times the keystroke is repeated as a result of the user's holding down the key.
		var scan_code		= (ushort)((int32Param&	  0xFF0000)>>16); //16 - 23The scan code.The value depends on the OEM.
		var extended_key	= 0!=  ((int32Param&  0x1000000)>>24); //24 Indicates whether the key is an extended key, such as a function key or a key on the numeric keypad.The value is 1 if the key is an extended key; otherwise, it is 0.
		var reserved		= (ushort)((int32Param&	0x1E000000)>>25); //25 - 28Reserved.
		var context_code	= 0!=  ((int32Param&	0x20000000)>>29); //29The context code.The value is 1 if the ALT key is down; otherwise, it is 0.
		var previous_key_state=0!= ((int32Param&	0x40000000)>>30); //30The previous key state.The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
		var transition_state= 0!=  ((int32Param&	0x80000000)>>31);//31	The transition state. The value is 0 if the key is being pressed and 1 if it is being released.

		if( !previous_key_state && !transition_state)//有键按下
		{
			Debug.Log ("钩子键盘按下:"+kc);
		}
		else if( previous_key_state && transition_state)
		{
			Debug.Log ("钩子键盘抬起:"+kc);
		}
		return 1;
	}

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

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值