vb.net disable ctrl esc ...- 특정키 동작 설정
*
Imports System.Runtime.InteropServices
Imports System.Reflection
Public Class Form1
Private Sub Form1_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleCreated
KeyboardJammer.Jam()
End Sub
Private Sub Form1_HandleDestroyed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleDestroyed
KeyboardJammer.UnJam()
End Sub


End Class
Public Class KeyboardJammer
Private Delegate Function HookCallback(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
Private Shared HookDelegate As HookCallback
Private Shared HookId As Integer
Private Const Wh_Keyboard_LL As Integer = 13
Private Const Vk_Tab As Integer = 9
Private Const Vk_Escape As Integer = 27
Private Const vk_Del As Integer = 46
Private Const Vk_F4 As Integer = 115
Private Const VK_LWinKey As Integer = 91
Private Const VK_RWinKey As Integer = 92
Private Shared Function KeyBoardHookProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Integer
'All keyboard events will be sent here.
'Don't process just pass along.
If nCode < 0 Then
Return CallNextHookEx(HookId, nCode, wParam, lParam)
End If
'Extract the keyboard structure from the lparam
'This will contain the virtual key and any flags.
'This is using the my.computer.keyboard to get the
'flags instead
Dim KeyboardSruct As KBDLLHOOKSTRUCT = Marshal.PtrToStructure(lParam, GetType(KBDLLHOOKSTRUCT))
MsgBox(KeyboardSruct.vkCode.ToString, MsgBoxStyle.OkOnly)
If KeyboardSruct.vkCode = Vk_Tab And My.Computer.Keyboard.AltKeyDown Then
'Alt Tab
Return 1
ElseIf KeyboardSruct.vkCode = Vk_Escape And My.Computer.Keyboard.CtrlKeyDown Then
'Control Escape
Return 1
ElseIf KeyboardSruct.vkCode = VK_LWinKey Or KeyboardSruct.vkCode = VK_RWinKey Then
'Left Windows Key or Rigth Windows Key
Return 1
ElseIf KeyboardSruct.vkCode = vk_Del And My.Computer.Keyboard.CtrlKeyDown And My.Computer.Keyboard.AltKeyDown Then
'Ctrl Alt Del
Return 1
ElseIf KeyboardSruct.vkCode = Vk_F4 And My.Computer.Keyboard.AltKeyDown Then
'Alt F4
Return 1
End If
'Send the message along
Return CallNextHookEx(HookId, nCode, wParam, lParam)
End Function
Public Shared Sub Jam()
'Add the low level keyboard hook
If HookId = 0 Then
HookDelegate = AddressOf KeyBoardHookProc
HookId = SetWindowsHookEx(Wh_Keyboard_LL, HookDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly.GetModules()(0)), 0)
If HookId = 0 Then
'error
End If
End If
End Sub
Public Shared Sub UnJam()
'Remove the hook
UnhookWindowsHookEx(HookId)
End Sub
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
Private Shared Function CallNextHookEx( _
ByVal idHook As Integer, _
ByVal nCode As Integer, _
ByVal wParam As IntPtr, _
ByVal lParam As IntPtr) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
Private Shared Function SetWindowsHookEx( _
ByVal idHook As Integer, _
ByVal HookProc As HookCallback, _
ByVal hInstance As IntPtr, _
ByVal wParam As Integer) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall, SetLastError:=True)> _
Private Shared Function UnhookWindowsHookEx(ByVal idHook As Integer) As Integer
End Function
Private Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As IntPtr
End Structure
End Class

동작 확인 필요

이 게시물을

댓글'1'

Option Strict On
Imports System.Runtime.InteropServices

Public Class Form1

    Private Const KEYEVENTF_EXTENDEDKEY As Long = &H1
    Private Const KEYEVENTF_KEYUP As Long = &H2
    Private Const VK_LWIN As Byte = &H5B
    Private Const VK_RWIN As Byte = &H5C
    Private Const VK_TAB As Byte = &H9
    Private Const VK_LMENU As Byte = &HA4
    Private Const VK_RMENU As Byte = &HA5
    Private Const VK_F4 As Byte = &H73
    Private Const VK_LCONTROL As Byte = &HA2


    Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Const WH_KEYBOARD_LL As Integer = 13
    Private Const WM_KEYUP As Integer = &H101
    Private Shared _proc As LowLevelKeyboardProc = AddressOf HookCallback
    Private Shared _hookID As IntPtr = IntPtr.Zero

    Public Declare Auto Function SetWindowsHookEx Lib "user32.dll" (ByVal idHook As Integer, ByVal lpfn As LowLevelKeyboardProc, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr

    Public Declare Auto Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hhk As IntPtr) As IntPtr

    Public Declare Auto Function CallNextHookEx Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    Public Declare Auto Function GetModuleHandle Lib "kernel32.dll" (ByVal lpModuleName As String) As IntPtr


    Private Shared Function SetHook(ByVal proc As LowLevelKeyboardProc) As IntPtr

        Dim curProcess As Process = Process.GetCurrentProcess()
        Dim curModule As ProcessModule = curProcess.MainModule

        Return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0)

    End Function

    Public Delegate Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

    Public Shared Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr

        If nCode >= 0 And wParam = CType(WM_KEYUP, IntPtr) Then
            Dim vkCode As Keys = CType(Marshal.ReadInt32(lParam), Keys)
            If vkCode = Keys.LWin Or vkCode = Keys.RWin Or vkCode = Keys.F4 Or vkCode = Keys.Tab Or vkCode = Keys.LShiftKey Or vkCode = Keys.RShiftKey Or vkCode = Keys.LControlKey Or vkCode = Keys.LMenu Then
                keybd_event(CByte(Keys.Zoom), 0, KEYEVENTF_EXTENDEDKEY, 0)

            End If
        End If

        Return CallNextHookEx(_hookID, nCode, wParam, lParam)
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        _hookID = SetHook(_proc)
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        UnhookWindowsHookEx(_hookID)
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        End
    End Sub
End Class

이 댓글을

공유하기

SEARCH

MENU NAVIGATION