VB.net 我的程序只有一个窗体,窗体很小,怎么才能检测到鼠标在窗体以外的范围移动,还有键盘输入
1个回答
展开全部
用API GetCursorPos
用Timer隔一段时间获取一次鼠标位置。
键盘输入,用 GetKeyboardState
加点分的话就给你完整的代码
Public Class NativeMethods
'获取鼠标位置
Declare Function GetCursorPos Lib "user32.dll" _
(ByRef lpPoint As System.Drawing.Point) as boolean
Private Shared keyState() As Byte
<DllImport("user32.dll")> _
Private Shared Function GetKeyboardState(ByVal keyState() As Byte) As Boolean
End Function
Private Shared Sub Update()
keyState = New Byte(256) {}
Dim result As Boolean = GetKeyboardState(keyState)
' Check for error:
If result = False Then
Debug.WriteLine("GetKeyBoardState error: " & Marshal.GetLastWin32Error)
Throw New Exception("GetKeyBoardState error: " & Marshal.GetLastWin32Error)
End If
End Sub
Public Enum LightState
Off
[On]
End Enum
' Example - the keyboard lights...
Public Shared ReadOnly Property CapsLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.CapsLock) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
Public Shared ReadOnly Property NumLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.NumLock) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
Public Shared ReadOnly Property ScrollLockState() As LightState
Get
Update()
Dim isOn As Boolean = (keyState(Keys.Scroll) = 1)
Return IIf(isOn, LightState.On, LightState.Off)
End Get
End Property
End Class
pinvoke.net: GetCursorPos (user32)
http://www.pinvoke.net/default.aspx/user32/GetCursorPos.html
pinvoke.net: GetKeyboardState (user32)
http://www.pinvoke.net/default.aspx/user32/GetKeyboardState.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询