LzmTW.uSystem.uWindows.Win32API+KeyStatusWather

Author:水如烟  

 

Imports  LzmTW.uSystem.uWindows.SafeNative

Namespace  LzmTW.uSystem.uWindows.Win32API

    
Public   Class  KeyStatusWather

        
Public   Event  KeyStatusChanged  As  KeyStatusWatcherHandler

        
Private   WithEvents  gKeyboardHook  As   New  MouseKeyboardHook
        
Private   WithEvents  gTimer  As   New  System.Timers.Timer( 1000 )

        
Private  gIsInitialize  As   Boolean

        
Protected   Overrides   Sub  Finalize()
            
Try
                ServiceDispose()
                gTimer.Dispose()
            
Finally
                
MyBase .Finalize()
            
End   Try
        
End Sub

        
Public   Sub  ServiceBegin()
            gKeyboardHook.Start(MouseKeyboardHook.HookWay.Keyboard)
            gTimer.Start()
        
End Sub

        
Public   Sub  ServiceDispose()
            gKeyboardHook.Stop()
            gTimer.Stop()
        
End Sub

        
' 取各键现值
         Public   Sub  ServceInitialze()
            gIsInitialize 
=   True

            OnKeyStatusChanged(
False , Keys.NumLock)
            OnKeyStatusChanged(
False , Keys.CapsLock)
            OnKeyStatusChanged(
False , Keys.Scroll)
            OnKeyStatusChanged(
False , Keys.Insert)
            OnKeyStatusChanged(
True Nothing )

            gIsInitialize 
=   False
        
End Sub

        
Private   Sub  gKeyboardHook_KeyDown( ByVal  sender  As   Object ByVal  e  As  System.Windows.Forms.KeyEventArgs)  Handles  gKeyboardHook.KeyDown
            OnKeyStatusChanged(
False , e.KeyCode)
        
End Sub

        
Private   Sub  mTimer_Elapsed( ByVal  sender  As   Object ByVal  e  As  System.Timers.ElapsedEventArgs)  Handles  gTimer.Elapsed
            OnKeyStatusChanged(
True Nothing )
        
End Sub

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

        
Private   Sub  OnKeyStatusChanged( ByVal  isTime  As   Boolean ByVal  formKey  As  System.Windows.Forms.Keys)
            
Dim  mEventArgs  As  KeyStatusWatcherEventArgs  =   Nothing

            
If  isTime  Then
                mEventArgs 
=   New  KeyStatusWatcherEventArgs(Now)
            
Else
                
Select   Case  formKey
                    
Case  Keys.NumLock, Keys.CapsLock, Keys.Scroll, Keys.Insert
                        
If  gIsInitialize  Then
                            mEventArgs 
=   New  KeyStatusWatcherEventArgs(FormKeyToKeyWatcher(formKey),  Not  KeyIsEnabled(formKey))
                        
Else
                            mEventArgs 
=   New  KeyStatusWatcherEventArgs(FormKeyToKeyWatcher(formKey), KeyIsEnabled(formKey))
                        
End   If
                
End   Select
            
End   If

            
If  mEventArgs  Is   Nothing   Then   Exit Sub
            
If  KeyStatusChangedEvent  Is   Nothing   Then   Exit Sub

            KeyStatusChangedEvent.Invoke(
Nothing , mEventArgs)
        
End Sub

        
Private   Function  FormKeyToKeyWatcher( ByVal  formKey  As  System.Windows.Forms.Keys)  As  KeyWatcher
            
Select   Case  formKey
                
Case  Keys.NumLock
                    
Return  KeyWatcher.NumLock
                
Case  Keys.CapsLock
                    
Return  KeyWatcher.CapsLock
                
Case  Keys.Scroll
                    
Return  KeyWatcher.ScrollLock
                
Case  Keys.Insert
                    
Return  KeyWatcher.Insert
                
Case   Else
                    
Return  KeyWatcher.Other
            
End   Select
        
End Function
    
End Class

End Namespace

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值