Globales KeyDown-Event

  • C#

Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Globales KeyDown-Event

    Tach Community,


    Habe eine Frage, es gibt bei einer WinForms das Event "KeyDown" diese Event wird jedoch nur aufgerufen wenn die "Form" aktiv ist.

    Jedoch möchte ich eine Art Keybinder machen und wenn ich in einem Spiel bin das ein Tastendruck erkannt wird.

    Wie funktioniert das am besten? Einen Timer ist klar aber was darin?


    MFG


    Verschoben nach Sonstige Problemstellungen
    -Artentus

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Artentus“ ()

    Wieso Timer? Globalen Hotkey setzen, RegisterHotKey und fertig.
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Hallo, ich hatte noch diesen Code rumliegen, leider in VB aber das sollte trotzdem kein Problem sein - Es ist nicht allzu komplex.
    GlobalInputHooks

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Imports System.Drawing
    3. Public Class GlobalInputHooks
    4. #Region "Native Methods"
    5. Private Declare Unicode Function GetModuleHandleW Lib "kernel32.dll" (ByVal lpModuleName As IntPtr) As IntPtr
    6. Private Declare Unicode Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Integer, ByVal lpfn As HOOKPROCDelegateKey, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
    7. Private Declare Unicode Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Integer, ByVal lpfn As HOOKPROCDelegateMouse, ByVal hMod As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
    8. Private Declare Unicode Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hhk As IntPtr) As UInteger
    9. Private Declare Unicode Function CallNextHookEx Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
    10. Private Declare Unicode Function CallNextHookEx Lib "user32.dll" (ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As IntPtr
    11. #End Region
    12. #Region "Hook Delagates"
    13. Private Delegate Function HOOKPROCDelegateKey(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
    14. Private Delegate Function HOOKPROCDelegateMouse(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As IntPtr
    15. Private Shared HookProcKey As New HOOKPROCDelegateKey(AddressOf KeyboardHookProc)
    16. Private Shared HookProcMouse As New HOOKPROCDelegateMouse(AddressOf MouseHookProc)
    17. #End Region
    18. #Region "Constants"
    19. Private Const WH_KEYBOARD_LL As Integer = 13
    20. Private Const WH_MOUSE_LL As Integer = 14
    21. Private Const HC_ACTION As Integer = 0
    22. #End Region
    23. #Region "Handles"
    24. Private Shared mHandleKey As IntPtr
    25. Private Shared mHandleMouse As IntPtr
    26. #End Region
    27. #Region "Structures"
    28. <StructLayout(LayoutKind.Sequential)> _
    29. Private Structure KBDLLHOOKSTRUCT
    30. Public vkCode As UInt32
    31. Public scanCode As UInt32
    32. Public flags As KBDLLHOOKSTRUCTFlags
    33. Public time As UInt32
    34. Public dwExtraInfo As UIntPtr
    35. End Structure
    36. <Flags()> _
    37. Private Enum KBDLLHOOKSTRUCTFlags As UInt32
    38. LLKHF_EXTENDED = &H1
    39. LLKHF_INJECTED = &H10
    40. LLKHF_ALTDOWN = &H20
    41. LLKHF_UP = &H80
    42. End Enum
    43. <StructLayout(LayoutKind.Sequential)> _
    44. Private Structure MOUSEHOOKSTRUCT
    45. Public pt As Point
    46. Public hwnd As Integer
    47. Public wHitTestCode As Integer
    48. Public dwExtraInfo As Integer
    49. End Structure
    50. #End Region
    51. #Region "Properties"
    52. Public Shared Property KeyHookEnable() As Boolean
    53. Get
    54. Return mHandleKey <> IntPtr.Zero
    55. End Get
    56. Set(ByVal value As Boolean)
    57. If KeyHookEnable = value Then Return
    58. If value Then
    59. mHandleKey = SetWindowsHookExW(WH_KEYBOARD_LL, HookProcKey, GetModuleHandleW(IntPtr.Zero), 0)
    60. Else
    61. UnhookWindowsHookEx(mHandleKey)
    62. mHandleKey = IntPtr.Zero
    63. End If
    64. End Set
    65. End Property
    66. Public Shared Property MouseHookEnable() As Boolean
    67. Get
    68. Return mHandleMouse <> IntPtr.Zero
    69. End Get
    70. Set(ByVal value As Boolean)
    71. If MouseHookEnable = value Then Return
    72. If value Then
    73. mHandleMouse = SetWindowsHookExW(WH_MOUSE_LL, HookProcMouse, GetModuleHandleW(IntPtr.Zero), 0)
    74. Else
    75. UnhookWindowsHookEx(mHandleMouse)
    76. mHandleMouse = IntPtr.Zero
    77. End If
    78. End Set
    79. End Property
    80. #End Region
    81. #Region "Hook Handling Functions"
    82. Private Shared Function KeyboardHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As KBDLLHOOKSTRUCT) As IntPtr
    83. 'lParam beinhaltet zusätzliche Informationen über den Hookcall in Form von einer KBDLLHOOKSTRUCT
    84. ' Return New IntPtr(1) zum Verschlucken des Calls,
    85. ' Return CallNextHookEx(mHandleKey, nCode, wParam, lParam) zum Durchlassen
    86. Return New IntPtr(1)
    87. End Function
    88. Private Shared Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByRef lParam As MOUSEHOOKSTRUCT) As IntPtr
    89. 'lParam beinhaltet zusätzliche Informationen über den Hookcall in Form von einer MOUSEHOOKSTRUCT
    90. ' Return New IntPtr(1) zum Verschlucken des Calls,
    91. ' Return CallNextHookEx(mHandleMouse, nCode, wParam, lParam) zum Durchlassen
    92. Return New IntPtr(1)
    93. End Function
    94. #End Region
    95. End Class


    Diese statische Klasse kann Systemweit Tastatur- und Mausevents abfragen und -fangen. Pass also auf was Du tust und sperr dich nicht aus - Ich übernehme keine Verantwortung für eventuell entstandene Schäden.
    SᴛᴀʀGᴀᴛᴇ01
    @AtomTiger Erläutere mal bitte Deinen Plan.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!