如何在VB.NET中使用ToAscii()API函数 [英] How to use ToAscii() API function in VB.NET

查看:121
本文介绍了如何在VB.NET中使用ToAscii()API函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
实现SetWindowsHookex()函数时遇到麻烦.

我将使用SetWIndowsHookex()函数记录按键操作.
而且我确定关键事件已正确钩住.

因此,我必须使用ToAscii()函数知道按下了哪个键.

这是我的代码段.

Hi, everyone.
I have trouble while implementing SetWindowsHookex() function.

I gonna log key pressing, using SetWIndowsHookex() function.
And I''m sure key event is hooked correctly.

So I have to know which key is pressed, using ToAscii() function.

Here are my snippet of my code.

Dim Key As String
Dim ks(256) As Byte
Dim b As Long
b = GetKeyboardState(ks(0))
Dim w As Long, scan As Long
scan = 0
ToAscii(lParam.vkCode, lParam.scanCode, ks(0), w, 0)

Key = ChrW(CType(w, Integer))



但是效果不好.

请帮帮我.

谢谢.



But it didn''t work well.

Please help me.

Thanks.

推荐答案

不能很好地工作"是什么意思?在调试器下运行它时,您按了什么键,lParam的值是什么?您确实在调试器下运行了它,对吧?
What does "didn''t work well" mean? When you ran it under the debugger, what key did you press, and what was the value of lParam? You did run it under the debugger, right?


要知道按下了哪个键,您无需将任何内容转换为ASCII.顺便说一句,请记住.NET内部支持Unicode UTF-16, ASCII.

lParam.vkCode中的内容与字符代码不直接相关;这是按键的虚拟代码".

这是这些代码的表: http://msdn.microsoft.com /en-us/library/dd375731(v=vs.85).aspx [
没有这样的转换.如果您不需要它,则只需要比较键即可.

让我解释. 虚拟代码不包含有关Unicode字符的信息,因为该信息不存在.字符尚未生成.虚拟键码代表物理键盘设备上的键,而不是字符.例如,如果您按下标记为"{"的键,则稍后将生成一个字符,但是该字符可以是"{","["或某些非ASCII字符,具体取决于多个字符.因素:修改键的状态(例如Shift),键切换(例如大写,大写锁定),已安装和已激活的键盘布局,应用程序的Unicode或非Unicode模式,当前输入语言,输入方法,应用程序的当前UI文化最后,在当前活动的应用程序中对事件进行本地处理 OS 会考虑所有这些因素以生成字符.

在您收到虚拟钥匙密码时,此信息(一般情况下)不可用.您只获得有关按下的物理键的信息.现在,即使字母键说英语"A"与标记为"A"的虚拟键具有相同的整数代码点值,也不意味着将生成字符"A".生成的字符(如果有)取决于我在上段中列出的因素.许多键根本不产生字符,并且始终以其虚拟键模式形式使用:F1至F12,Alt,Shift,VolumeMute,VolumeDown-其中许多.请参阅我上面引用的表格.

通常,键盘处理分为几层,一层基于另一层:扫描代码,OEM代码,虚拟键代码.虚拟密钥代码不在最硬件"级别.例如,如果包含与硬件立场无关的鼠标事件.

—SA
To know which key is pressed, you don''t need to convert anything to ASCII. By the way, keep in mind that .NET internally supports Unicode UTF-16, not ASCII.

What you have in lParam.vkCode in not directly related to character codes; this is a "virtual code" of the key press.

Here is the table of those codes: http://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx[^].

You can take this table in C# code, make a enumeration type out of it and use. Actually, you should get the same enumeration as System.Windows.Input.Key or System.Windows.Forms.Keys. You can use one of those types and type cast.



Answering a follow-up question: "Would you like to explain me about the way how to convert Virtual code to Unicode?"

There is no such conversion. In you case you don''t need it, you need to compare just keys.

Let me explain. Virtual code carry no information about Unicode characters because this information is simply absent. The character is not yet generated. Virtual key codes represent the keys on the physical keyboard device, not character. For example, if you press the key marked as ''{'', later on a character will be generated, but this character can be ''{'', ''['' or some non-ASCII character, depending on several factors: status of modifier keys (like Shift), key toggles (like Capital, Caps Lock), installed and activated keyboard layout, Unicode or non-Unicode mode of the application, current input language, input method, current UI culture of the application and, finally, local processing of the events in a currently active application All that factors are taken into account by OS to generate a character.

At the moment you receive a virtual key code, this information — in general case — is not available. You only got an information of the physical key pressed. Now, even though alphabetical key, say English ''A'' has the same integer code point value as the virtual key marked as ''A'', it does not mean a character ''A'' will be generated. A generated character (if any) depends on the factors I listed in the above paragraph. Many key generate no characters at all and are always used in their virtual key mode form: F1 to F12, Alt, Shift, VolumeMute, VolumeDown — many of them. Please see the table I referenced above.

Generally, there are several layers of keyboard processing, one based on another: scan code, OEM code, virtual key code. Virtual key code in not the "most hardware" level. For example, if contains mouse event which are unrelated from the hardware stand point.

—SA


这篇关于如何在VB.NET中使用ToAscii()API函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆