0

Is there a way to Click the ENTER key on the keyboard, other than SendKeys.Send("{ENTER}"? I have an Application I created in VB.net that will create a line of text then send it to another application with AppActivate, it works great when the receiving application was created in C++. But when the receiving application was created in Java the SendKeys.Send("{ENTER}" will not work. All the text is transferred to the Java application but the ENTER button will not click. Is there another way to Click ENTER on the Keyboard or simulate it?

Thank You

Malachilee
  • 99
  • 1
  • 11
  • Have you tried a short delay before sending {ENTER}? Just to test, you could add `Threading.Thread.Sleep(500)` after sending the text but before sending {ENTER}. – Andrew Morton Dec 09 '19 at 15:11
  • I have that in place already, I put it up to 5000 just to see but it will not click. I've also tried "~" instead of "{ENTER}" but no luck. – Malachilee Dec 09 '19 at 15:15

1 Answers1

0

Have you tried P/Invoke?

For example:

<DllImport("user32.dll", SetLastError:=True, EntryPoint:="PostMessageA")>
Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Boolean

Public Const WM_KEYDOWN As Integer = &H0100 'Key Down Event
Public Const VK_RETURN As Integer = &H0D 'Enter Key

Public Shared Sub SendKeyDownEvent(ByVal hWnd As IntPtr, ByVal key As Integer)
    PostMessage(hWnd, WM_KEYDOWN, key, 0)
    System.Threading.Thread.Sleep(500)
End Sub

And you could call it like:

SendKeyDownEvent(handleToApplication, VK_RETURN)

There are plenty of resources available for obtaining handles to other applications:

Dekryptid
  • 1,062
  • 1
  • 9
  • 21
  • Remember to send a `WM_KEYUP` afterwards as well. – Visual Vincent Dec 09 '19 at 18:10
  • @Dekryptid Thanks for sending me in a new direction. I'm trying to get this working but getting some errors. DllImport says declaration expected and a few more errors. I'm still learning, so it could be something I'm missing. I'll keep working with it. – Malachilee Dec 10 '19 at 13:57
  • @Andrew Morton I'm using 2019 – Malachilee Dec 10 '19 at 17:39
  • @Malachilee Just in case you haven't done it yet, it's best to set [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) as the default. A side effect of that is that it lets VS give you more help with coding, and so it might suggest ways to fix the errors you're encountering. – Andrew Morton Dec 10 '19 at 18:47