Wyłączenie klawisza klawiatury w całym Windows

0

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?

1

Zapewne źle szukasz po ja wizę że Google jednak coś znajduje szukaj po frazie
Low Level Keyboard Hook C#
a jak nie to mogę niedrogo przetłumaczyć ten kod na C#

0

Dzięki @kAzek
Znalazłem takie coś --> https://www.codeproject.com/articles/19004/a-simple-c-global-low-level-keyboard-hook
Po lekkim przerobieniu kodu uzyskałem efekty taki, jak chciałem :)

Jakby komuś się przydał to tu zamieszczam przerobiony kod z dwoma przyciskami (gdy naciśnie się button 1, to wtedy blokuje przycisk wyznaczony w linijce 28 i 29)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Utilities;

namespace key_preview {
	public partial class Form1 : Form {
		globalKeyboardHook gkh = new globalKeyboardHook();

		public Form1() {
			InitializeComponent();
            
		}
        int a = 6;
        


		private void Form1_Load(object sender, EventArgs e) {
            


                
                    gkh.HookedKeys.Add(Keys.A);
                    gkh.HookedKeys.Add(Keys.B);
                    gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
                    gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);

                



            

            
		}

		void gkh_KeyUp(object sender, KeyEventArgs e) {
            if (a == 1)
            {
                e.Handled = true;
            }
		}

		void gkh_KeyDown(object sender, KeyEventArgs e) {
            if (a == 1)
            {
                e.Handled = true;
            }
		}

        private void button1_Click(object sender, EventArgs e)
        {
            a = 1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

     
	}
}
 

1 użytkowników online, w tym zalogowanych: 0, gości: 1