Emulate real keyboard key press
Here is a quick description of my problem:
I am trying to connect my Arduino programmable board with Microsoft Flight Simulator (3D) using vb.net app to communicate between them.
By rotating the encoder on the Arduino, board sends a signal via COM (USB) port to the computer. My vb.net application reads the port and needs to send a keystroke to Flight Simulator.
I have already tried using the "send key" function:
Code:
SetForegroundWindow(FsimHandle)
SendKeys.SendWait("h")
I do get output in Notepad and similar programs, but my Flight Simulator (ran in windowed mode) simply does not register it. If I try using the MS On-screen keyboard, however, I do get the desired effect. Also pressing the key on the real keyboard works ::D
The problem is that Flight Simulator only accepts KEY inputs, and I just don't know how to implement this using vb.net.
Kindest regards,
Nikola
Re: Emulate real keyboard key press
SendKeys sends keystrokes to the currently active window. if your arduino controller has a window, there are other methods available too.
First try giving focus to the window before SendKeys:
AppActivate("window title")
or:
AppActivate(window handle)
edit: it's the edit control that needs to be focussed
Re: Emulate real keyboard key press
Quote:
Originally Posted by
.paul.
edit: it's the edit control that needs to be focussed
Thank you for replying,
I did not really understand what you mean by this ^
I have tried AppActivate function, but only thing it does is focus the Flight Simulator window. Unfortunately no keys are passed on.
A virtual keyboard would be a perfect solution, only I don't know does such thing exist.
Re: Emulate real keyboard key press
ok. try this:
Code:
Public Class Form1
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
Private Const WM_SETTEXT As Integer = &HC
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendMessage(windowHandle, WM_SETTEXT, 0, "h")
End Sub
End Class
Re: Emulate real keyboard key press
if that doesn't help, could you post a screenshot of your Flight Simulator window?