Announcement

Collapse
No announcement yet.

Dienst: Element mit Focus nach Tastendruck kopieren

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dienst: Element mit Focus nach Tastendruck kopieren

    Hallo,

    ich soll einen Dienst in VB.NET schreiben, der auf eine Tastenkombination wartet, die Daten des Elementes, das momentan den Focus hat kopiert und weiter verarbeitet. Im konkreten Fall geht es um eine Zeile in einem Datagrid einer VB 6 Anwendung.

    Hat jemand einen Tipp oder einen Link?

    Vielen Dank im Voraus!

  • #2
    Also was Du willst is quasi ein Keylogger. Ich hab sowas mal gebraucht um ein bestimmtes Tastenkürzel Systemweit abzufangen. Das geht nur mit der user-API von Windows und damit nur ab NT4 SP6(?). Ich benutz dafür einfach folgende Klasse:
    Code:
    Public Class KeyboardLowLevelHook
        Implements IDisposable
    
        Private Declare Function UnhookWindowsHookEx Lib "user32" _
          (ByVal hHook As Integer) As Integer
    
        Private Declare Function SetWindowsHookEx Lib "user32" _
          Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
          ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
          ByVal dwThreadId As Integer) As Integer
    
        Private Declare Function GetAsyncKeyState Lib "user32" _
          (ByVal vKey As Integer) As Integer
    
        Private Declare Function CallNextHookEx Lib "user32" _
          (ByVal hHook As Integer, _
          ByVal nCode As Integer, _
          ByVal wParam As Integer, _
          ByVal lParam As KBDLLHOOKSTRUCT) As Integer
    
        Private Structure KBDLLHOOKSTRUCT
            Public vkCode As Integer
            Public scanCode As Integer
            Public flags As Integer
            Public time As Integer
            Public dwExtraInfo As Integer
        End Structure
    
        Private Const HC_ACTION As Integer = 0
        Private Const LLKHF_EXTENDED As Integer = &H1
        Private Const LLKHF_INJECTED As Integer = &H10
        Private Const LLKHF_ALTDOWN As Integer = &H20
        Private Const LLKHF_UP As Integer = &H80
    
        'KeyUp/KeyDown constants
        Private Const WM_KEYDOWN = &H100
        Private Const WM_KEYUP = &H101
        Private Const WM_SYSKEYDOWN = &H104
        Private Const WM_SYSKEYUP = &H105
    
        Private Const WH_KEYBOARD_LL As Integer = 13&
        Private KeyboardHandle As Integer
        Private disposed As Boolean = False
    
        Private Delegate Function KeyboardHookDelegate( _
          ByVal Code As Integer, _
          ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
                       As Integer
    
         Public Event KeyDown As KeyEventHandler
        Public Event KeyUp As KeyEventHandler
    
        <MarshalAs(UnmanagedType.FunctionPtr)> _
        Private callback As KeyboardHookDelegate = New KeyboardHookDelegate(AddressOf KeyboardCallback)
    
        Private Function KeyboardCallback(ByVal Code As Integer, _
          ByVal wParam As Integer, _
          ByRef lParam As KBDLLHOOKSTRUCT) As Integer
    
            If (Code = HC_ACTION) Then
                'Call the appropriate event
    
                Dim KeyData As Keys
                Dim args As KeyEventArgs
    
                KeyData = lParam.vkCode
    
                 If CBool(GetAsyncKeyState(Keys.Menu) And &H8000) Then
                    KeyData = KeyData Or Keys.Alt
                End If
    
                 If CBool(GetAsyncKeyState(Keys.ControlKey) And &H8000) Then
                     KeyData = KeyData Or Keys.Control
                End If
    
                If CBool(GetAsyncKeyState(Keys.ShiftKey) And &H8000) Then
                     KeyData = KeyData Or Keys.Shift
                End If
    
                 args = New KeyEventArgs(KeyData)
    
                If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
                    OnKeyDown(Me, args)
                ElseIf wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
                    OnKeyUp(Me, args)
                End If
    
                If args.Handled Then
                    Return 1
                End If
            End If
    
            Return CallNextHookEx(KeyboardHandle, _
              Code, wParam, lParam)
    
        End Function
    
        Protected Sub OnKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            RaiseEvent KeyDown(sender, e)
        End Sub
    
        Protected Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
            RaiseEvent KeyUp(sender, e)
        End Sub
    
        Public Overridable Sub Dispose() Implements IDisposable.Dispose
            If Not disposed Then
                disposed = True
    
                UnhookWindowsHookEx(KeyboardHandle)
            Else
                Throw New ObjectDisposedException("KeyboardHook")
            End If
        End Sub
    
        Public Sub New()
     
            KeyboardHandle = SetWindowsHookEx( _
                WH_KEYBOARD_LL, callback, _
                Marshal.GetHINSTANCE( _
                [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
        End Sub
    
        Protected Overrides Sub Finalize()
            MyBase.Finalize()
    
            If Not disposed Then
                Dispose()
            End If
        End Sub
    End Class
    Verwenden kannst Du die dann einfach mit
    Code:
    Imports System.Runtime.InteropServices
    Imports System.Reflection
    Imports System.Threading
        Dim WithEvents hook As New KeyboardLowLevelHook

    Comment


    • #3
      Also was Du willst is quasi ein Keylogger. Ich hab sowas mal gebraucht um ein bestimmtes Tastenkürzel Systemweit abzufangen. Das geht nur mit System-Hooks über die user-API von Windows und damit nur ab NT4 SP6(?). Ich benutz dafür einfach folgende Klasse:
      Code:
      Public Class KeyboardLowLevelHook
          Implements IDisposable
      
          Private Declare Function UnhookWindowsHookEx Lib "user32" _
            (ByVal hHook As Integer) As Integer
      
          Private Declare Function SetWindowsHookEx Lib "user32" _
            Alias "SetWindowsHookExA" (ByVal idHook As Integer, _
            ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, _
            ByVal dwThreadId As Integer) As Integer
      
          Private Declare Function GetAsyncKeyState Lib "user32" _
            (ByVal vKey As Integer) As Integer
      
          Private Declare Function CallNextHookEx Lib "user32" _
            (ByVal hHook As Integer, _
            ByVal nCode As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As KBDLLHOOKSTRUCT) As Integer
      
          Private Structure KBDLLHOOKSTRUCT
              Public vkCode As Integer
              Public scanCode As Integer
              Public flags As Integer
              Public time As Integer
              Public dwExtraInfo As Integer
          End Structure
      
          Private Const HC_ACTION As Integer = 0
          Private Const LLKHF_EXTENDED As Integer = &H1
          Private Const LLKHF_INJECTED As Integer = &H10
          Private Const LLKHF_ALTDOWN As Integer = &H20
          Private Const LLKHF_UP As Integer = &H80
      
          'KeyUp/KeyDown constants
          Private Const WM_KEYDOWN = &H100
          Private Const WM_KEYUP = &H101
          Private Const WM_SYSKEYDOWN = &H104
          Private Const WM_SYSKEYUP = &H105
      
          Private Const WH_KEYBOARD_LL As Integer = 13&
          Private KeyboardHandle As Integer
          Private disposed As Boolean = False
      
          Private Delegate Function KeyboardHookDelegate( _
            ByVal Code As Integer, _
            ByVal wParam As Integer, ByRef lParam As KBDLLHOOKSTRUCT) _
                         As Integer
      
           Public Event KeyDown As KeyEventHandler
          Public Event KeyUp As KeyEventHandler
      
          <MarshalAs(UnmanagedType.FunctionPtr)> _
          Private callback As KeyboardHookDelegate = New KeyboardHookDelegate(AddressOf KeyboardCallback)
      
          Private Function KeyboardCallback(ByVal Code As Integer, _
            ByVal wParam As Integer, _
            ByRef lParam As KBDLLHOOKSTRUCT) As Integer
      
              If (Code = HC_ACTION) Then
                  'Call the appropriate event
      
                  Dim KeyData As Keys
                  Dim args As KeyEventArgs
      
                  KeyData = lParam.vkCode
      
                   If CBool(GetAsyncKeyState(Keys.Menu) And &H8000) Then
                      KeyData = KeyData Or Keys.Alt
                  End If
      
                   If CBool(GetAsyncKeyState(Keys.ControlKey) And &H8000) Then
                       KeyData = KeyData Or Keys.Control
                  End If
      
                  If CBool(GetAsyncKeyState(Keys.ShiftKey) And &H8000) Then
                       KeyData = KeyData Or Keys.Shift
                  End If
      
                   args = New KeyEventArgs(KeyData)
      
                  If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Then
                      OnKeyDown(Me, args)
                  ElseIf wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
                      OnKeyUp(Me, args)
                  End If
      
                  If args.Handled Then
                      Return 1
                  End If
              End If
      
              Return CallNextHookEx(KeyboardHandle, _
                Code, wParam, lParam)
      
          End Function
      
          Protected Sub OnKeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
              RaiseEvent KeyDown(sender, e)
          End Sub
      
          Protected Sub OnKeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
              RaiseEvent KeyUp(sender, e)
          End Sub
      
          Public Overridable Sub Dispose() Implements IDisposable.Dispose
              If Not disposed Then
                  disposed = True
      
                  UnhookWindowsHookEx(KeyboardHandle)
              Else
                  Throw New ObjectDisposedException("KeyboardHook")
              End If
          End Sub
      
          Public Sub New()
       
              KeyboardHandle = SetWindowsHookEx( _
                  WH_KEYBOARD_LL, callback, _
                  Marshal.GetHINSTANCE( _
                  [Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
          End Sub
      
          Protected Overrides Sub Finalize()
              MyBase.Finalize()
      
              If Not disposed Then
                  Dispose()
              End If
          End Sub
      End Class
      Den Hook erstellst du einfach mit
      Code:
          Imports System.Runtime.InteropServices
          Imports System.Reflection
          Imports System.Threading
          Dim WithEvents hook As New KeyboardLowLevelHook
      Um die Tastendrücke abzufangen mußt du dann bloß noch die folgende Funktion mit deinem Code füllen:
      Code:
      Private Sub hook_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles hook.KeyDown
      
      End Sub
      Und hinterher entfernen nicht vergessen:
      Code:
          hook.Dispose()
      Ich halte das aber nicht unbedingt für eine ideale Lösung, da Systemweite Hooks sehr Ressourcenlastig sind.
      Mit dem auslesen der Daten aus einem Fremdfenster kann ich Dir grad nicht helfen. Aber wenn ich mich recht erinnere hat jedes Element in VB6 einrn eigenen Handle (hwnd), den man sicher irgendwie rauskriegen kann.

      Comment


      • #4
        Originally posted by kernelkiller View Post
        ich soll einen Dienst in VB.NET schreiben, der auf eine Tastenkombination wartet, die Daten des Elementes, das momentan den Focus hat kopiert und weiter ...
        Schon mal überlegt das dies auf 99,9% der Vista-Rechner nicht mehr gehen wird da hier Dienste keinen Zugriff mehr auf GUI-Elemente haben da Dienst welche auf GUI-Zugriff haben ein potentielles Sicherheitsproblem darstellen.

        Comment

        Working...
        X