cancel
Showing results for 
Search instead for 
Did you mean: 

How to use "Sendkeys" to press ENTER on a Windows dialog?

leon_laikan
Participant
0 Kudos

Hi, everybody.

Our team is working on an SDK add-on which will print a PLD Report linked to the  Inventory Transfer Screen in SAP B1.

We have successfully done the following steps using UI API (i.e ActivateMenuItem)

  • Opened the Inventory Transfer Screen in SAP B1
  • Pressed the Print button in SAP B1 (SBO_Application.ActivateMenuItem("520")

This opens the Windows Dialog. We must now press "ENTER" to print the PLD Report.

How to do this?

-------------

We found the answer here. It is exactly our problem.

Answer provided by:

It seems to work for the person who asked the question, but it does not work for us.

We have no idea what went wrong. Any suggestions  to help us find what's wrong?

Where must we put this 3rd section in our VB.NET Program?

--------------

' // Now implement the timer eventhandler (thread safe, you can send any keys like ENTER which closes the print dialog box and ESC when you  would like to cancel

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)

        Dim lHwnd As Integer = GetForegroundWindow

        If lCurHwnd = lHwnd Then Exit Sub

        ' Send enter key to Commit the dialog window

        System.Windows.Forms.SendKeys.SendWait("{ENTER}")

        'Stop timer

        _timer.Enabled = False

    End Sub

--------------

Thanks

Leon

Added:

If there is another way to print the PLD Report without popping up the Windows Print Dialog, this will simplify my coding:

I am thinking of the following alternatives:

  • Press the Printer Icon on the toolbar
  • Use shortcut Ctrl + Shift + P

Could anyone give me the SDK coding to effect these (or give me an indication how to do these)

Thanks

Leon

======================================================================

For convenience, we copy the full solution provided by Janos Nagy below

======================================================================

Logic:

You should implement a multi-threaded program, which is looking for the operating system actual window, and compares with the SAP B1 Window ID. If differs, then you can send an enter key. (click on default button).

You can build up thread safe procedure with custom code, but i have used there a timer, which starts when you send the printing, and stops when enter has been sent.

Add the following code to your addon variables declare section:

  Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr

  Dim _timer As New System.Timers.Timer

  Dim lCurHwnd As Integer

When you activating the Print Menu, add the following code

' SAP B1 Window ID via Windows API Call

        lCurHwnd = GetForegroundWindow()

        _timer.Interval = 100

        AddHandler _timer.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Me.Timer1_Elapsed)

        _timer.Enabled = True

        'RockAndRoll

        SBO_Application.ActivateMenuItem("520")

Now impement the timer eventhandler (thread safe, you can send any keys like ENTER which closes the print dialog box and ESC when you would like to cancel

Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)

        Dim lHwnd As Integer = GetForegroundWindow

        If lCurHwnd = lHwnd Then Exit Sub

        ' Send enter key to Commit the dialog window

        System.Windows.Forms.SendKeys.SendWait("{ENTER}")

        'Stop timer

        _timer.Enabled = False

    End Sub

======================================================================

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Dear Leon Lai Kan

try this

System.Windows.Forms.SendKeys.Send("{^}{+}(p)")

System.Windows.Forms.SendKeys.Send("{ENTER}")


Regards,

Nadeemkhan

leon_laikan
Participant
0 Kudos

Hi, Nadeem

Thanks a lot for your reply.

I will be back in Office on Monday, and then I will try your code.

Best Regards,

Leon

Answers (0)