Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of tonyedmo
tonyedmo

simulate key press combinations .....Alt+3
Ok im working on the program that send key commans to another application.
It seems this application doesn't see Alt or Control combinations using
.nets SendKeys.Send
or windows APIs:SendMessage, SendInput, keybd_event, or SetKeyboardState

most of these methods work fine when sending a "normal" key
but when a combination is send the application reacts as if the Alt key was not sent

I have been looking all over the place and it seems it might be because the application the receive the key events uses directx

Any one have any experience with this type of issue.........fyi im working in C#

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Dirk HaestDirk Haest🇧🇪

To specify keys combined with any combination of the SHIFT, CTRL, and ALT keys, precede the key code with one or more of the following codes.

Key Code
SHIFT +
CTRL ^
ALT %

To specify that any combination of SHIFT, CTRL, and ALT should be held down while several other keys are pressed, enclose the code for those keys in parentheses. For example, to specify to hold down SHIFT while E and C are pressed, use "+(EC)". To specify to hold down SHIFT while E is pressed, followed by C without SHIFT, use "+EC".

http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(vs.71).aspx


SendKeys.Send "C" --> will send  C
SendKeys.Send "^C" --> will send ctrl+C

Avatar of tonyedmotonyedmo

ASKER

as I said I have tried using sendkeys.send

Avatar of Dirk HaestDirk Haest🇧🇪

Are you sure that the program that needs to retrieve the keys has the focus ?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Dirk HaestDirk Haest🇧🇪

In the program I've done, I did this
using System.Runtime.InteropServices;

hWnd = Win32.FindWindow(strClassName, strAppTitle);
if (hWnd != 0)
{
    bWindow = Win32.SetForegroundWindow(hWnd);
    System.Windows.Forms.SendKeys.SendWait("{TAB}");
}

Avatar of Dirk HaestDirk Haest🇧🇪

Forgot, I also needed this class:
      public class Win32

      {
            [DllImport("user32.dll")]
            public static extern int FindWindow(
                  string lpClassName,  // class name
                  string lpWindowName  // window name
                  );

            [DllImport("user32.dll")]
            public static extern bool SetForegroundWindow(int hWnd);

      }

yeah im positive it has focus
works with normal key sends, its just when you try and send something like Alt+3 or Sendkeys.send("%(3)")
the direct x application only sees the 3
I spent got portion of time trying to get sendkeys to work as I thought perhaps i was useing wrong
Then I moved on to the APIs listed above.

Another posibly interesting note is spy++ does not see any keyboard messages go to the app

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Dirk HaestDirk Haest🇧🇪

Can you try it with {} instead of ()

So something like this: "%{3}"

tried that too :)

figured it out
used sendinput api and put a delay in between the down and up messages

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of Dirk HaestDirk Haest🇧🇪

Glad you figured it out.
By the way: did you try SendKeys.SendWait or only SendKeys.Send ?
(In the program I've written with the sendkeys I also used small delays between every sendkeys)

ended up having to do somthing like the following
sendinput  alt keydown
sendinput  3  keydown
sleep 50
sendinput 3 keyup
sendinput alt keyup

ASKER CERTIFIED SOLUTION
Avatar of Dirk HaestDirk Haest🇧🇪

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of Computer101Computer101🇺🇸

Forced accept.

Computer101
EE Admin

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.

.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.