none
How to handle keybd_event to send keypress to app directly in VB?

    Question

  • Hi!

    I want to use keystrokes in emulator, explaining:

    Emulated keyboard is mapped to real keyboard, so key events are taken from real keyboard.

    I want to use key combinations on not mapped keys - example:
    - in emulator CTRL+E changes character color to white - this is mapped in emulator TAB+E > so it requires send TAB+E by KeybdEvent, this is not problem, but emulator doesn't any action
    - in emulator Esc and then X changes active desktop - this is mapped in emulator F1 then X > requires to send F1 keypress and then X keypress by KeybdEvent, this is not problem, but emulator doesn't any action
    - I used key F9 as key which displays directory - it sends to emulator clipboard content in which is command DIR and <Enter> - this works...

    I created little program in Visual Basic which simulates keypress - works, but like on background - like global shortcuts, not to specific app and tested it for emulator - again no action even if is window focused, global keys sends correctly.
    I need to use key direct send to any app.

    KeybdEvent I used in VB code below - how to correct it for direct keypress?

    Public Class Form1
    
        Dim Cnt, F, I, A As Short
        Dim Val As Int16
    
        Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
        Const KEYEVENTF_KEYUP = &H2
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            On Error Resume Next
    
            If My.Application.CommandLineArgs.Count = 0 Then
                End
            Else
                Cnt = My.Application.CommandLineArgs.Count
            End If
    
            Dim Arg(Cnt) As String
            Dim Flag(Cnt) As Boolean
            Dim Part1(Cnt) As Int16
            Dim Part2(Cnt) As Int16
    
            For F = 1 To Cnt
                Arg(F) = My.Application.CommandLineArgs(F - 1)
            Next
    
            For F = 1 To Cnt
                Flag(F) = False
                A = Len(Arg(F))
                For I = 1 To A
                    If Mid(Arg(F), I, 1) = "+" Then Flag(F) = True : Part1(F) = CByte(Mid(Arg(F), 1, (I - 1))) : Part2(F) = CByte(Mid(Arg(F), (I + 1), A)) : Exit For
                Next I
            Next F
    
            For F = 0 To Cnt
                Execute(Part1(F), Part2(F), Flag(F))
            Next
    
            End
        End Sub
    
        Private Sub Execute(Key1 As Byte, Key2 As Byte, Combo As Boolean)
    
            On Error Resume Next
    
            If Combo = False Then Goto Simple
    
            PressKey(Key1)
            PressKey(Key2)
            ReleaseKey(Key2)
            ReleaseKey(Key1)
            Exit Sub
    Simple:
            PressKey(Key1)
            ReleaseKey(Key1)
        End Sub
    
        Private Sub PressKey(e As Byte)
    
            On Error Resume Next
    
            keybd_event(e, 0, 0, 0)
        End Sub
    
        Private Sub ReleaseKey(e As Byte)
    
            On Error Resume Next
    
            keybd_event(e, 0, KEYEVENTF_KEYUP, 0)
        End Sub
    End Class

    Thank you for each help.

    Miro


    • Edited by mksft Tuesday, March 21, 2017 11:30 PM
    Tuesday, March 21, 2017 11:28 PM

Answers

All replies

  • Hi

    Can't show any code, but, if I am not mistaken, something like this would be my first approach.

    I would create a map of the desired key code to be sent against the real keyboard key code.

    I would create a dictionary using the real keyboard key code as the dictionary key and the required emulator key code as the dictionary value.

    Then, in the real keyboard, when a key is used, send the dictionary value for that key code.

    Just my initial thoughts.


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, March 22, 2017 12:01 AM
    Tuesday, March 21, 2017 11:59 PM
  • I created little program in Visual Basic which simulates keypress - works, but like on background - like global shortcuts, not to specific app and tested it for emulator - again no action even if is window focused, global keys sends correctly.

    If some work and some don't, then the place to look for the problem is in the key codes you are sending, or the test for simple and compound.

    You will not be able to debug your application while you are using Resume Next.  Remove the error trapping so that you can detect and fix the errors.

    I presume your Execute is being executed from somewhere else - as posted, that code won't do anything.

    Wednesday, March 22, 2017 12:23 AM
  • I don't understand correctly.

    But now it is solved using AutoHotKey scripting.
    Key sending works.

    Miro

    Wednesday, March 22, 2017 1:46 PM
  • Hi mksft,

    It seems that your issue has been solved by yourself, please close your thread by "Mark as answer" to help more community members to search this answer quickly.

    Thanks for your understanding.

    Best regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 23, 2017 7:33 AM
    Moderator
  • Hi mksft,

    It seems that your issue has been solved by yourself, please close your thread by "Mark as answer" to help more community members to search this answer quickly.

    Thanks for your understanding.

    Best regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Cherry,

    He/She did it with something outside VB so you can move this in my opinion to OT.

    Very bad as this stays in the VB for Net community forums. Search engines will point to it. 


    Success
    Cor

    Thursday, March 23, 2017 8:08 AM
  • I am sorry that I solved it outside VB, I'm programming in VB, but on AHK forum I found solution...
    Here I found not, so had I to leave it as unanswered?

    Miro

    Thursday, March 23, 2017 11:21 AM