Witam.
Na internecie znalazłem taki kod na zablokowanie wybranego przycisku klawiatury w całym Windowsie:
Option Strict On
Imports System.Runtime.InteropServices
Imports System.Diagnostics
Imports System.Reflection
Public Class Form1
Private Structure KBDLLHOOKSTRUCT
Public key As Keys
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public extra As IntPtr
Public ada As IntPtr
End Structure
'System level functions to be used for hook and unhook keyboard input
Private Delegate Function LowLevelKeyboardProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function SetWindowsHookEx(ByVal id As Integer, ByVal callback As LowLevelKeyboardProc, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function UnhookWindowsHookEx(ByVal hook As IntPtr) As Boolean
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function CallNextHookEx(ByVal hook As IntPtr, ByVal nCode As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function GetModuleHandle(ByVal name As String) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function GetAsyncKeyState(ByVal key As Keys) As Short
End Function
'Declaring Global objects
Private ptrHook As IntPtr
Private objKeyboardProcess As LowLevelKeyboardProc
Public Sub New()
Try
Dim objCurrentModule As ProcessModule = Process.GetCurrentProcess().MainModule
'Get Current Module
objKeyboardProcess = New LowLevelKeyboardProc(AddressOf captureKey)
'Assign callback function each time keyboard process
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0)
'Setting Hook of Keyboard Process for current module
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Catch ex As Exception
End Try
End Sub
Private Function captureKey(ByVal nCode As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
Try
If nCode >= 0 Then
Dim objKeyInfo As KBDLLHOOKSTRUCT = DirectCast(Marshal.PtrToStructure(lp, GetType(KBDLLHOOKSTRUCT)), KBDLLHOOKSTRUCT)
Dim ada = 2
If (True) Then
If objKeyInfo.key = Keys.RWin OrElse objKeyInfo.key = Keys.RShiftKey Then
' Disabling Windows keys
Return CType(1, IntPtr)
End If
End If
If objKeyInfo.key = Keys.ControlKey OrElse objKeyInfo.key = Keys.Escape Then
' Disabling Ctrl + Esc keys
Return CType(1, IntPtr)
End If
End If
Return CallNextHookEx(ptrHook, nCode, wp, lp)
Catch ex As Exception
End Try
End Function
End Class
Problem jest taki, że ja kompletnie nie znam Visual Basic'a. Chciałbym takie coś w C# - linijke kodu na blokowanie danego przycisku w Windowsie. Szukałem czegoś podobnego do C#, ale nic nie znalazłem. Pomożecie?