Announcement

Collapse
No announcement yet.

Feedback: Sending keystrokes with SendInput

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

    #21
    > Would it be possible to find that handle by enumerating windows

    As long as you have your WinAPI reference open today, why don't you turn to the pages where GetWindowThreadProcessID() and OpenProcess() are described?
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


      #22
      Originally posted by Michael Mattias View Post
      ..why don't you turn to the pages where GetWindowThreadProcessID() and OpenProcess() are described?
      I did exactly that and those indeed seem like the functions required. Thanks !

      Kind regards
      Eddy

      Comment


        #23
        Oops, I thought one of my demos showed WaitForInputIdle... but it doesn't.
        Last edited by Michael Mattias; 29 May 2009, 08:56 AM. Reason: My bad
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


          #24
          Hi Eddy!
          I was giving your code a try, but I'm not getting any results.

          I opened a small PB app that has nothing but an edit control. Then I started your app on my Win7 PC, and pressed Ctrl J. When I keep typing, nothing appears.

          What should I be seeing?

          I also tried opening 2 instances of the app as well. Same results.

          Likewise I opened Notepad and then pressed Ctrl-J. Nothing.

          Can you give me some guidance?

          Comment


            #25
            Originally posted by Gary Beene View Post
            Hi Eddy!
            I was giving your code a try, but I'm not getting any results.
            What should I be seeing?
            If you have Notepad open and it has focus, pressing CTRL J should paste the following 4 lines of text in Notepad:
            First line
            next line
            PowerBASIC
            éééé
            Pressing CTRL K should bring up an inputbox asking to enter some text. Pressing enter closes the inputbox and pastes the entered text in the active text window (Notepad).

            I just tried in Win XP and Win 7 and works fine here.
            Maybe the hotkey combinations CTRL J and CTRL K are already used on your system by another application?
            Try replacing DlgProc of my little demo program by this DlgProc.
            When you run the program, you have 9 seconds to make sure Notepad (or another text window) has focus.
            It will paste the lines of text without you having to do anything.

            Code:
            '-------------------------------------------------------------------------
            CALLBACK FUNCTION DlgProc
                STATIC nAtom()  AS DWORD
                LOCAL  sText    AS STRING
                LOCAL  iRet     AS LONG
                LOCAL  iCount   AS LONG
               
                SELECT CASE CBMSG
                    CASE %WM_INITDIALOG
                    
                        SLEEP 9000   'Make sure Notepad has focus before this time has elapsed...
                        WaitForNoKeys  'use this to wait for user to let go of keys (so our keys dont end up as ctrl keys)
                        SendString "First-{BACKSPACE} line{ENTER}next line{ENTER}"
                        SendString "Pooo{BACKSPACE}{BACKSPACE}werBASIC{ENTER}" 
                        SendString "éééé{ENTER}"
                             
            END FUNCTION
            Kind regards
            Eddy

            Comment


              #26
              Thanks Eddy,
              I'll be away from my desk for a bit (tennis match) but will look at it again when I return.

              Comment


                #27
                Im thinking about how to send the keystrokes to the previous focused window (i want to do a toolbar and i wish to send the keystrokes to the notepad).

                One choice is send this string "{ALT_D}{TAB}{ALT_U}" with a small delay, and start sending the keystrokes, like this:

                Code:
                          WaitForNoKeys  'use this to wait for user to let go of keys (so our keys dont end up as ctrl keys)
                          SendString "{ALT_D}{TAB}{ALT_U}"
                          SLEEP 100
                          SendString "Hello World{ENTER}"
                is there some elegant way to do this?

                i post an image for clarify, sorry my english.

                still no code, this is a mockup (no real windows injured during my tests)


                PD: another question, should i translate REDIM nAtom(1:2) to REDIM nAtom(2) OR (1 TO 2) to compile with pb 10.3?

                Code:
                    STATIC nAtom()  AS DWORD
                    LOCAL  sText    AS STRING
                    LOCAL  iRet     AS LONG
                    LOCAL  iCount   AS LONG
                
                    SELECT CASE CBMSG
                        CASE %WM_INITDIALOG
                            iRet = TIMER
                            REDIM nAtom(1:2)  ' HERE I GET A COMPILER ERROR
                            nAtom(1) = GLOBALADDATOM ("SendInput_demo2" + STR$(iRet + 1))
                            nAtom(2) = GLOBALADDATOM ("SendInput_demo2" + STR$(iRet + 2))
                            REGISTERHOTKEY CBHNDL, nAtom(1), 2, ASC("Y") ' Ctrl-J
                            REGISTERHOTKEY CBHNDL, nAtom(2), 2, ASC("K") ' Ctrl-K
                Attached Files
                Last edited by Roberto Porcar; 9 Aug 2012, 09:26 AM.

                Comment


                  #28
                  Roberto,

                  In this link:


                  Look for function 'AppActivate'. It allows you to specify a window caption name to make that window receive focus.

                  Kind regards
                  Eddy

                  Comment


                    #29
                    if you know the window title ( or a subset of the window title) of the target window to receive the keystrokes, then Eddy right that the best way to activate the target window is the of the API. ( note that if you have two windows of the same name, then the API will activate the one that was most recently active... which is the behavior that we want).

                    Here is a post with another sample program for activating the target window and then sending keystrokes to it


                    But if the program needs to be more flexible to send keystrokes to the previous active window regardless of that window's title, then the only solution that I found is to first send keystrokes to alt-tab to that window, allowing nominal sleep, and then send the rest of the keystrokes... just like you proposed.

                    Comment


                      #30
                      Thanks for the replies.

                      I would to show the last active window (i know this can give me problems because some text editors change the name of the title when the file is modified and not saved).

                      Code:
                      'Compilable Example:
                      #COMPILER PBWIN 10
                      #COMPILE EXE
                      #DIM ALL
                      %Unicode=1 : %IDC_Button = 500
                      #INCLUDE "win32api.inc"
                      
                      %ID_Timer1 = 500
                      
                      GLOBAL hDlg,hFound AS DWORD, szText AS WSTRINGZ * %MAX_PATH, temp AS WSTRINGZ * %MAX_PATH
                      
                      GLOBAL hForeground AS DWORD
                      
                      
                      FUNCTION PBMAIN() AS LONG
                         hForeground = GetForegroundWindow()
                         DIALOG NEW PIXELS, 0, "Get Last Active Window",300,300,420,100,%WS_OVERLAPPEDWINDOW TO hDlg
                         CONTROL ADD LABEL, hDlg, 205, "Start", 5, 50, 400, 15, %SS_LEFT
                         CONTROL ADD BUTTON, hDlg, %IDC_Button, "Check now!", 10,10,120,20
                         DIALOG SHOW MODAL hDlg CALL DlgProc
                      END FUNCTION
                      
                      CALLBACK FUNCTION DlgProc() AS LONG
                         SELECT CASE CB.MSG
                      
                            CASE %WM_INITDIALOG
                               SetTimer(CB.HNDL, %ID_Timer1, 500, BYVAL %NULL)   'uses callback messages
                      
                            CASE %WM_TIMER
                               IF CB.WPARAM = %ID_Timer1 THEN
                                     hForeground = GetForegroundWindow()
                                     GetWindowText(hForeground, temp, SIZEOF(szText))
                                     IF temp <> "Get Last Active Window" THEN
                                       szText = temp
                                     END IF
                                     CONTROL SET TEXT hDlg, 205, szText
                               END IF
                      
                            CASE %WM_COMMAND
                               IF CB.CTL = %IDC_Button AND CB.CTLMSG = %BN_CLICKED THEN
                                   GetWindowText(hForeground, szText, SIZEOF(szText))
                                   'msgbox szText
                                   CONTROL SET TEXT hDlg, 205, szText
                               END IF
                         END SELECT
                      END FUNCTION
                      this code its all that i get until now, based on a Gary sample. ill try to see first if the last focused window exists and if not found ill try to sendkeys to the "alt+tab" window.

                      But, whats the best choice to know about some window was focused? i have to set a timer and check at some fast intervals or there is some api?

                      feel free to atack my code... this is my first PB project.
                      Last edited by Roberto Porcar; 9 Aug 2012, 04:14 PM.

                      Comment

                      Working...
                      X
                      😀
                      🥰
                      🤢
                      😎
                      😡
                      👍
                      👎