Systemweite Tastenkombination abfragen

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Samus Aran.

    Systemweite Tastenkombination abfragen

    Hallo,

    ich möchte eine Tastenkombination wie z.B. Strg+F5 systemweit, also dass die Form auch im Hintergrund sein kann und trotzdem auf den Tastendruck reagiert.
    Ich habe schon oft danach gegoogelt und in diversen Foren gesucht, aber noch nichts funktionierendes gefunden.
    Kann mir da jemand helfen bitte?!

    Liebe Grüße,
    chrismueller
    Da gibts 2 Möglichkeiten. Einmal die RegisterHotkey-API - damit kann man codeseitig dasselbe machen, als wenn du einer Verknüpfung auf dem Desktop einen Shortcut zufügst.
    Ich hab da mal ein Sample in C# zu gemacht RegisterHotkey

    (Wesentlich aufwändiger ist was mit tastatur-hooks zu machen, ich find, man sollte sowas gar nicht veröffentlichen, denn mit tastaturhooks kann man auch passwortsniffer bauen.)
    OK, Dankeschön ErfinderDesRades!
    Ich habe dein Sample in C# jetzt nach VB.NET übersetzen lassen, es funktioniert wunderbar!

    Hier dann nochmal der komplette übersetzte Code für VB.NET.

    In einer neuen Klasse "Hotkey":

    VB.NET-Quellcode

    1. Imports System.Collections.Generic
    2. Imports System.ComponentModel
    3. Imports System.Runtime.InteropServices
    4. Public NotInheritable Class Hotkey
    5. Inherits NativeWindow
    6. Private Declare Ansi Function RegisterHotKey Lib "user32" (ByVal Hwnd As IntPtr, ByVal ID As Integer, ByVal Modifiers As Integer, ByVal Key As Integer) As Integer
    7. Private Declare Ansi Function UnregisterHotKey Lib "user32" (ByVal Hwnd As IntPtr, ByVal ID As Integer) As Integer
    8. Public Event Pressed As EventHandler
    9. Private _Value As Integer = 0
    10. Private Const _WinKey As Integer = CInt(Keys.Alt) << 1
    11. Public Sub New()
    12. Me.CreateHandle(New CreateParams())
    13. End Sub
    14. Public Property Value() As Keys
    15. Get
    16. Return CType(_Value, Keys)
    17. End Get
    18. Set(ByVal value As Keys)
    19. If _Value = CInt(value) Then
    20. Return
    21. End If
    22. If _Value <> 0 AndAlso UnregisterHotKey(Me.Handle, _Value) = 0 Then
    23. Throw New Exception("Fehler")
    24. End If
    25. _Value = CInt(value)
    26. If _Value = 0 Then
    27. Return
    28. End If
    29. Dim ApiModifier = 0
    30. If Keys.Shift = (value And Keys.Shift) Then
    31. ApiModifier += 4
    32. End If
    33. If Keys.Control = (value And Keys.Control) Then
    34. ApiModifier += 2
    35. End If
    36. If Keys.Alt = (value And Keys.Alt) Then
    37. ApiModifier += 1
    38. End If
    39. If _WinKey = (_Value And _WinKey) Then
    40. ApiModifier += 8
    41. End If
    42. If RegisterHotKey(Me.Handle, _Value, ApiModifier, _Value And &HFFFF) = 0 Then
    43. Throw New Exception("Ist der Key schon von einer anderen Anwendung registriert?")
    44. End If
    45. End Set
    46. End Property
    47. Protected Overrides Sub WndProc(ByRef m As Message)
    48. Const WM_HOTKEY As Integer = &H312
    49. If m.Msg = WM_HOTKEY Then
    50. RaiseEvent Pressed(Me, EventArgs.Empty)
    51. End If
    52. MyBase.WndProc(m)
    53. End Sub
    54. End Class


    In der Form:

    VB.NET-Quellcode

    1. Public WithEvents HK As New Hotkey
    2. Sub Form_Load() Handles MyBase.Load
    3. HK.Value = Keys.? 'Tastenkombination festlegen; für Strg+F5 wäre das Keys.Control + Keys.F5
    4. End Sub
    5. Sub HK_Pressed() Handles HK.Pressed
    6. 'Das hier wird ausgeführt, wenn die Tastenkombination gedrückt wurde!
    7. End Sub