重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
窗体内检测按键
专注于为中小企业提供成都做网站、成都网站设计、成都外贸网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业招远免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上1000家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True '注册窗体的键盘事件
End Sub
在TextBox1中显示按键代码
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
TextBox1.Text = Str(e.KeyCode) + vbCrLf
End Sub
窗体没有焦点,用窗体监测键盘输入缓冲区可以解决,但这似乎可以用于监测用户输入内容,不要用于非法目的呀!所以就是知道也不会告诉你的。
最简单的加速运动示例,直接拷贝代码,即可用方向键控制控件移动
Public Class 最简单的加速运动
Dim 左右, 上下 As Integer
Dim X, Y, VX, VY, VA As Double
Dim WithEvents 时钟 As New Timer
Dim WithEvents 移动控件 As New Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
VA = 0.5
X = 300.0
Y = 300.0
移动控件.BackColor = Color.MediumPurple
移动控件.Size = New Size(60, 60)
Controls.Add(移动控件)
时钟.Interval = 25
时钟.Enabled = True
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then 上下 = -1
If e.KeyCode = Keys.Down Then 上下 = 1
If e.KeyCode = Keys.Left Then 左右 = -1
If e.KeyCode = Keys.Right Then 左右 = 1
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
If e.KeyCode = Keys.Up Then 上下 = 0
If e.KeyCode = Keys.Down Then 上下 = 0
If e.KeyCode = Keys.Left Then 左右 = 0
If e.KeyCode = Keys.Right Then 左右 = 0
End Sub
Private Sub 时钟_Tick(sender As Object, e As EventArgs) Handles 时钟.Tick
VX = Math.Min(VX + 左右 * VA, 10)
VY = Math.Min(VY + 上下 * VA, 10)
X += VX
Y += VY
If X 0 Then X = 2 : VX = -VX
If Y 0 Then Y = 2 : VY = -VY
If X Width - 移动控件.Width Then X = Width - 移动控件.Width - 2 : VX = -VX
If Y Height - 移动控件.Height Then Y = Height - 移动控件.Height - 2 : VY = -VY
移动控件.Location = New Point(X, Y)
End Sub
End Class
这是不可能完成的任务,.NET平台不存在开发驱动的能力,你要想在驱动层对键盘操作,最好使用C,C++来实现,比较方便。 像现在一些比较出名的网络游戏,都是在驱动层进行保护的。
.net框架就是对API就行封装,做成一个个的类然后可以让你轻松调用,但是。net封装的内容,是常用的内容,有些东西用的时候,还是需要调用API函数,自己写,毕竟,微软不知道你要用什么函数,所以尽可能的满足用户大众口味而已。如果你需要用的比较复杂,还是调用API自己写类吧。
希望我的回答对您有所帮助
微软的DirectInput是基于硬件底层,可以在窗口无焦点时捕捉按键,有兴趣可以去了解了解,在游戏制作方面很有用。
DirectX SDK 中文版,里面有关于DirectInput的介绍与用法。
嗯,蛮巧的,前两天也在玩这个
不过我是用作PS2的扫描抢输入处理的。我偷偷懒不改程序了,您自己参考一下把,主要使用API:SetWindowsHookEx来实现,您只需要修改以下CallHookProc这个回调中的内容即可,其中我是让它遇到回车就RaiseEvent的,当然您也可以修改默认构造函数,使得初始化的时候传递一个函数方法的地址进来(就是那个GetBarCodeEvent委托声明,然后在创建实例的时候使用AddressOf来传递函数地址)
实现类:
======================
Imports System.Reflection
Imports System.Threading
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Text
Public Class MyHook
Private Structure KeyboardHookStruct
Dim vkCode As Integer
Dim ScanCode As Integer
Dim Flags As Integer
Dim Time As Integer
Dim DwExtraInfo As Integer
End Structure
Private Const WH_JOURNALRECORD = 0
Private Const WH_JOURNALPLAYBACK = 1
Private Const WH_KEYBOARD = 2
Private Const WH_GETMESSAGE = 3
Private Const WH_CALLWNDPROC = 4
Private Const WH_CBT = 5
Private Const WH_SYSMSGFILTER = 6
Private Const WH_MOUSE = 7
Private Const WH_HARDWARE = 8
Private Const WH_DEBUG = 9
Private Const WH_SHELL = 10
Private Const WH_FOREGROUNDIDLE = 11
Private Const WH_CALLWNDPROCRET = 12
Private Const WH_KEYBOARD_LL = 13
Private Const WH_MOUSE_LL = 14
Private Const VK_SHIFT As Byte = H10
Private Const VK_CAPITAL As Byte = H14
Private Const VK_NUMLOCK As Byte = H90
Private Const WM_KEYDOWN = H100
Private Const WM_KEYUP = H101
Private Const WM_SYSKEYDOWN = H104
Private Const WM_SYSKEYUP = H105
Private Delegate Sub HookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr)
Public Delegate Sub GetBarCodeDelegate(ByVal _BarCode As String)
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As HookProc, ByVal hMod As IntPtr, ByVal dwThreadId As Integer) As Integer
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal idHook As Integer) As Integer
Private Declare Function CallNextHookEx Lib "user32" (ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
Public Event OnBarCodeEvent(ByVal _BarCode As String)
Private GetBarCodeEvent As GetBarCodeDelegate
Private KeyboardHookProcedure As HookProc
Private m_lHook As Integer = 0
Private DataBuff As String = ""
Private IsShift As Boolean = False
Public Sub New()
End Sub
Protected Overrides Sub Finalize()
StopHook()
MyBase.Finalize()
End Sub
'打开系统钩子,开始监视扫描枪
Public Sub StartSystemHook()
If m_lHook = 0 Then
KeyboardHookProcedure = New HookProc(AddressOf CallHookProc)
'Dim hMod() As System.Reflection.Module = System.Reflection.Assembly.GetExecutingAssembly.GetModules()
'm_lHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(hMod(0)), 0)
m_lHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)), 0)
End If
End Sub
'打开线程钩子,开始监视扫描枪
Public Sub StartThreadHook(ByVal threadID As Integer)
If m_lHook = 0 Then
KeyboardHookProcedure = New HookProc(AddressOf CallHookProc)
Dim hMod() As System.Reflection.Module = System.Reflection.Assembly.GetExecutingAssembly.GetModules
m_lHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE(hMod(0)), threadID)
End If
End Sub
'关闭监视
Public Sub StopHook()
If m_lHook 0 Then
UnhookWindowsHookEx(m_lHook)
End If
End Sub
Private Sub CallHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr)
If nCode = 0 And wParam = WM_KEYDOWN And Not isHookLock Then
Dim m As KeyboardHookStruct = Marshal.PtrToStructure(lParam, GetType(KeyboardHookStruct))
If m.vkCode = 48 And m.vkCode = 57 Then
'如果扫描的是数字
If Not IsShift Then
DataBuff += (m.vkCode - 48).ToString
Else
Select Case m.vkCode
Case 49
DataBuff += "!"
Case 50
DataBuff += "@"
Case 51
DataBuff += "#"
Case 52
DataBuff += "$"
Case 53
DataBuff += "%"
Case 54
DataBuff += "^"
Case 55
DataBuff += ""
Case 56
DataBuff += "*"
Case 57
DataBuff += "("
Case 48
DataBuff += ")"
End Select
End If
ElseIf (m.vkCode = 65 And m.vkCode = 90) Then
'如果扫描的是字母
DataBuff += Chr(m.vkCode).ToString
ElseIf (m.vkCode = 189) Or (m.vkCode = 109) Then
'如果扫描码是"-"
DataBuff += "-"
ElseIf m.vkCode = 187 Then
'如果扫描码是"="或"+"
If Not IsShift Then
DataBuff += "="
Else
DataBuff += "+"
End If
ElseIf m.vkCode = 190 Then
'如果扫描码是"."或""
If Not IsShift Then
DataBuff += "."
Else
DataBuff += ""
End If
ElseIf m.vkCode = 191 Then
'如果扫描码是"/"或"?"
If Not IsShift Then
DataBuff += "/"
Else
DataBuff += "?"
End If
ElseIf m.vkCode = 13 Then
'如果扫描的是回车
RaiseEvent OnBarCodeEvent(DataBuff.Replace(Chr(13), ""))
DataBuff = ""
Else
'DataBuff += " "
End If
If m.vkCode = 160 Then
'若有Shift键盘,就设置Shift为开状态,然后由其它流程就判断下一个键值是多少
IsShift = True
Else
IsShift = False
End If
End If
End Sub
End Class
============================
附赠键盘键值图,注意VB中Shift、Alt、Ctrl键值有些不一样,自己分步调试一下就能获得(Shift是160,其它您自己找把)