水如烟

                 顺其自然,水到渠成 LzmTW

文或代码皆是面向初学者.我是爱好者,也是初学者.那些"文章",只按自己理解写,我是不知术语名词的.所以只供参考,也仅供参考.

导航

Author:水如烟


这个类要利用上面的鼠标键盘钩子类

StatusKeySateWatcher.EventArgs.vb 事件参数定义

Namespace uWindows
    Partial 
Class StatusKeySateWatcher
        
Public Class EventArgs
            
Inherits System.EventArgs

            
Private gIsEnabled As Boolean
            
Private gMsgKey As MsgKeyEnum

            
Sub New(ByVal msgkey As MsgKeyEnum, ByVal isenabled As Boolean)
                gMsgKey 
= msgkey
                gIsEnabled 
= isenabled
            
End Sub

            
Public ReadOnly Property MsgKey() As MsgKeyEnum
                
Get
                    
Return gMsgKey
                
End Get
            
End Property

            
Public ReadOnly Property IsEnabled() As Boolean
                
Get
                    
Return gIsEnabled
                
End Get
            
End Property

        
End Class

        
Public Enum MsgKeyEnum
            NumLock
            CapsLock
            ScrollLock
            Insert
        
End Enum
    
End Class
End Namespace


StatusKeySateWatcher.vb 功能键状态指示类

Namespace uWindows
    
Public Class StatusKeySateWatcher
        
Public Event TimeChanged(ByVal now As DateTime)
        
Public Event KeySateChanged(ByVal sender As ObjectByVal e As StatusKeySateWatcher.EventArgs)

        
Private WithEvents gKeyboardHook As New uWindows.MouseKeyboardHook
        
Private WithEvents gMainForm As Windows.Forms.Form
        
Private WithEvents gTimer As New System.Timers.Timer(1000)

        
Sub New(ByVal mainform As Windows.Forms.Form)
            gMainForm 
= mainform
            Initialize()
        
End Sub

        
Private Sub Initialize()
            gKeyboardHook.Start(MouseKeyboardHook.HookWay.Keyboard)
            gTimer.Start()
        
End Sub

        
'取各键现值
        Private Sub GetOriginalState()
            
Dim sender As Object = Nothing
            
Dim mEventArgs As StatusKeySateWatcher.EventArgs = Nothing
            mEventArgs 
= New StatusKeySateWatcher.EventArgs(MsgKeyEnum.NumLock, Not KeyIsEnabled(Windows.Forms.Keys.NumLock))
            
RaiseEvent KeySateChanged(sender, mEventArgs)
            mEventArgs 
= New StatusKeySateWatcher.EventArgs(MsgKeyEnum.CapsLock, Not KeyIsEnabled(Windows.Forms.Keys.CapsLock))
            
RaiseEvent KeySateChanged(sender, mEventArgs)
            mEventArgs 
= New StatusKeySateWatcher.EventArgs(MsgKeyEnum.ScrollLock, Not KeyIsEnabled(Windows.Forms.Keys.Scroll))
            
RaiseEvent KeySateChanged(sender, mEventArgs)
            mEventArgs 
= New StatusKeySateWatcher.EventArgs(MsgKeyEnum.Insert, Not KeyIsEnabled(Windows.Forms.Keys.Insert))
            
RaiseEvent KeySateChanged(sender, mEventArgs)
        
End Sub

        
Protected Overrides Sub Finalize()
            gKeyboardHook.Stop()
            gTimer.Dispose()
            
MyBase.Finalize()
        
End Sub

        
Private Sub gMainForm_FormClosing(ByVal sender As ObjectByVal e As System.Windows.Forms.FormClosingEventArgs) Handles gMainForm.FormClosing
            
Me.Finalize()
        
End Sub

        
Private Sub gMainForm_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles gMainForm.Load
            
Me.GetOriginalState()
        
End Sub

        
Private Sub gKeyboardHook_KeyDown(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyEventArgs) Handles gKeyboardHook.KeyDown
            
Dim mEventArgs As StatusKeySateWatcher.EventArgs = Nothing
            
Select Case e.KeyCode
                
Case Windows.Forms.Keys.NumLock
                    mEventArgs 
= New StatusKeySateWatcher.EventArgs(MsgKeyEnum.NumLock, KeyIsEnabled(Windows.Forms.Keys.NumLock))
                    
RaiseEvent KeySateChanged(sender, mEventArgs)
                
Case Windows.Forms.Keys.CapsLock
                    mEventArgs 
= New StatusKeySateWatcher.EventArgs(MsgKeyEnum.CapsLock, KeyIsEnabled(Windows.Forms.Keys.CapsLock))
                    
RaiseEvent KeySateChanged(sender, mEventArgs)
                
Case Windows.Forms.Keys.Scroll
                    mEventArgs 
= New StatusKeySateWatcher.EventArgs(MsgKeyEnum.ScrollLock, KeyIsEnabled(Windows.Forms.Keys.Scroll))
                    
RaiseEvent KeySateChanged(sender, mEventArgs)
                
Case Windows.Forms.Keys.Insert
                    mEventArgs 
= New StatusKeySateWatcher.EventArgs(MsgKeyEnum.Insert, KeyIsEnabled(Windows.Forms.Keys.Insert))
                    
RaiseEvent KeySateChanged(sender, mEventArgs)
            
End Select

        
End Sub

        
Private Function KeyIsEnabled(ByVal keycode As Windows.Forms.Keys) As Boolean
            
Return Not CType(SafeNativeMethods.GetKeyState(keycode), Boolean)
        
End Function

        
Private Sub mTimer_Elapsed(ByVal sender As ObjectByVal e As System.Timers.ElapsedEventArgs) Handles gTimer.Elapsed
            
RaiseEvent TimeChanged(e.SignalTime)
        
End Sub

    
End Class
End Namespace


使用:

Public Class MainForm


    
Private WithEvents gKeyState As New LzmTW.uWindows.StatusKeySateWatcher(Me)

    
Private Sub gKeyState_KeySateChanged(ByVal sender As ObjectByVal e As LzmTW.uWindows.StatusKeySateWatcher.EventArgs) Handles gKeyState.KeySateChanged
        
Select Case e.MsgKey
            
Case LzmTW.uWindows.StatusKeySateWatcher.MsgKeyEnum.NumLock
                
Me.NumLockToolStripStatusLabel.Enabled = e.IsEnabled
            
Case LzmTW.uWindows.StatusKeySateWatcher.MsgKeyEnum.CapsLock
                
Me.CapsLockToolStripStatusLabel.Enabled = e.IsEnabled
            
Case LzmTW.uWindows.StatusKeySateWatcher.MsgKeyEnum.ScrollLock
                
Me.ScrollLockToolStripStatusLabel.Enabled = e.IsEnabled
            
Case LzmTW.uWindows.StatusKeySateWatcher.MsgKeyEnum.Insert
                
Me.InsertToolStripStatusLabel.Enabled = e.IsEnabled
        
End Select
    
End Sub


    
Private Sub gKeyState_TimeChanged(ByVal now As DateHandles gKeyState.TimeChanged
        
Me.TimeToolStripStatusLabel.Text = now.ToString
    
End Sub

End Class