Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Sending a keystroke to all applications

Sending a keystroke to all applications

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionjavascripthardwarehelptutorial
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Geary
    wrote on last edited by
    #1

    I have a piece of hardware with some buttons which when pressed will send an events via a USB interface to an application I have written(let's call it APP1). I have a 2nd application (let's call it APP2) that reacts to certain keystrokes (for example when it sees a Key Press event for SPACE BAR it will pop-up a dialog). I have no control over the code in this application. I need to make it so when APP1 sees a button is pressed on the hardware it will send a keystroke to APP2 (or to all windows). Some constraints: - I cannot change the hardware to simply send the SPACE BAR, I only have control over APP1. - There may exist another version of APP2 that will react to a different button such as ENTER key (or any key sequence for that matter) so I would like to have APP1 be configurable in the event it sends to other windows/applications and is in no way dependent on the name of APP2 as I may see many versions of APP2. What is the best way to handle this? Sendkeys? What information do I need to know about APP2 from within APP1 to send the keystroke to this window specifically? I'm OK with (and would prefer) sending this event to all Windows if that is possible so if APP2 changes in the future APP1 is not impacted. I'm fairly new to Windows Programming so all help and insight is appreciated. Thanks in advance. KG

    L P L A 4 Replies Last reply
    0
    • K Kevin Geary

      I have a piece of hardware with some buttons which when pressed will send an events via a USB interface to an application I have written(let's call it APP1). I have a 2nd application (let's call it APP2) that reacts to certain keystrokes (for example when it sees a Key Press event for SPACE BAR it will pop-up a dialog). I have no control over the code in this application. I need to make it so when APP1 sees a button is pressed on the hardware it will send a keystroke to APP2 (or to all windows). Some constraints: - I cannot change the hardware to simply send the SPACE BAR, I only have control over APP1. - There may exist another version of APP2 that will react to a different button such as ENTER key (or any key sequence for that matter) so I would like to have APP1 be configurable in the event it sends to other windows/applications and is in no way dependent on the name of APP2 as I may see many versions of APP2. What is the best way to handle this? Sendkeys? What information do I need to know about APP2 from within APP1 to send the keystroke to this window specifically? I'm OK with (and would prefer) sending this event to all Windows if that is possible so if APP2 changes in the future APP1 is not impacted. I'm fairly new to Windows Programming so all help and insight is appreciated. Thanks in advance. KG

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, you should not send anything to "all windows" or "all applications". A lot of applications and windows are meant for human interaction only, and may suddenly display a dialog, where some of not all keys get interpreted the same as clicking the OK button. You cannot possibly want that. you should send a keystroke or a sequence of keystrokes to a single window, after making sure you got the right window; and even that is not always fool proof, as it may change soon after you've checked. There are a couple of ways to do it: - System.Windows.Forms.SendKeys.SendWait() sends keys to the active app and window; - SendMessage() could be used to send regular Windows messages such as WM_KEYDOWN and WM_KEYUP. The former is managed but unsafe as, even when you get the ForeGroundWindow's title text and check it, the active window may change suddenly; the latter is a bit harder to use but should always be OK. I use both, and choose depending on circumstances. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      1 Reply Last reply
      0
      • K Kevin Geary

        I have a piece of hardware with some buttons which when pressed will send an events via a USB interface to an application I have written(let's call it APP1). I have a 2nd application (let's call it APP2) that reacts to certain keystrokes (for example when it sees a Key Press event for SPACE BAR it will pop-up a dialog). I have no control over the code in this application. I need to make it so when APP1 sees a button is pressed on the hardware it will send a keystroke to APP2 (or to all windows). Some constraints: - I cannot change the hardware to simply send the SPACE BAR, I only have control over APP1. - There may exist another version of APP2 that will react to a different button such as ENTER key (or any key sequence for that matter) so I would like to have APP1 be configurable in the event it sends to other windows/applications and is in no way dependent on the name of APP2 as I may see many versions of APP2. What is the best way to handle this? Sendkeys? What information do I need to know about APP2 from within APP1 to send the keystroke to this window specifically? I'm OK with (and would prefer) sending this event to all Windows if that is possible so if APP2 changes in the future APP1 is not impacted. I'm fairly new to Windows Programming so all help and insight is appreciated. Thanks in advance. KG

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Find a way to have App2 tell App1 that it wants these events.

        1 Reply Last reply
        0
        • K Kevin Geary

          I have a piece of hardware with some buttons which when pressed will send an events via a USB interface to an application I have written(let's call it APP1). I have a 2nd application (let's call it APP2) that reacts to certain keystrokes (for example when it sees a Key Press event for SPACE BAR it will pop-up a dialog). I have no control over the code in this application. I need to make it so when APP1 sees a button is pressed on the hardware it will send a keystroke to APP2 (or to all windows). Some constraints: - I cannot change the hardware to simply send the SPACE BAR, I only have control over APP1. - There may exist another version of APP2 that will react to a different button such as ENTER key (or any key sequence for that matter) so I would like to have APP1 be configurable in the event it sends to other windows/applications and is in no way dependent on the name of APP2 as I may see many versions of APP2. What is the best way to handle this? Sendkeys? What information do I need to know about APP2 from within APP1 to send the keystroke to this window specifically? I'm OK with (and would prefer) sending this event to all Windows if that is possible so if APP2 changes in the future APP1 is not impacted. I'm fairly new to Windows Programming so all help and insight is appreciated. Thanks in advance. KG

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Take a look at these articles here and here. :)

          Life is a stage and we are all actors!

          modified on Thursday, June 3, 2010 5:22 PM

          L 1 Reply Last reply
          0
          • L Lost User

            Take a look at these articles here and here. :)

            Life is a stage and we are all actors!

            modified on Thursday, June 3, 2010 5:22 PM

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            :confused: APP2 was not to be changed.

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            L 1 Reply Last reply
            0
            • L Luc Pattyn

              :confused: APP2 was not to be changed.

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I didn't understand the question completely, I've thought that it's about top level windows, which is wrong.Unfortunately SendKeys works only with active application so maybe enumerating all top level windows and their child controls is required so I would strike my previous post. :^)

              Life is a stage and we are all actors!

              1 Reply Last reply
              0
              • K Kevin Geary

                I have a piece of hardware with some buttons which when pressed will send an events via a USB interface to an application I have written(let's call it APP1). I have a 2nd application (let's call it APP2) that reacts to certain keystrokes (for example when it sees a Key Press event for SPACE BAR it will pop-up a dialog). I have no control over the code in this application. I need to make it so when APP1 sees a button is pressed on the hardware it will send a keystroke to APP2 (or to all windows). Some constraints: - I cannot change the hardware to simply send the SPACE BAR, I only have control over APP1. - There may exist another version of APP2 that will react to a different button such as ENTER key (or any key sequence for that matter) so I would like to have APP1 be configurable in the event it sends to other windows/applications and is in no way dependent on the name of APP2 as I may see many versions of APP2. What is the best way to handle this? Sendkeys? What information do I need to know about APP2 from within APP1 to send the keystroke to this window specifically? I'm OK with (and would prefer) sending this event to all Windows if that is possible so if APP2 changes in the future APP1 is not impacted. I'm fairly new to Windows Programming so all help and insight is appreciated. Thanks in advance. KG

                A Offline
                A Offline
                Anshul R
                wrote on last edited by
                #7

                Enumerate all processes See if the process has a UI(to distinguish from other background processes) The code will be like

                For Each x As Process In Process.GetProcesses
                If x.MainWindowTitle <> "" And x.MainWindowTitle <> Me.Text Then
                AppActivate(x.Id) 'If you know the application's title, AppActivate("Title") will also work
                SendKeys.SendWait("KEYS YOU WANT TO SEND")
                End If
                Next
                AppActivate(Me.Text)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups