Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:16:05+00:00 2026-06-15T07:16:05+00:00

Possible Duplicate: C# and SendMessage (keys) is not working I am writing an application

  • 0

Possible Duplicate:
C# and SendMessage (keys) is not working

I am writing an application that sends keystrokes to another application using the SendMessage function defined in user32.dll. I have figured out how to send a single keystroke but I am stumped trying to send the keystroke along with the ALT key.

For the purposes of my question I will focus on sending F1, and ALT + F1.

As stated above, I am able to send the F1 key no problem. Here is a snippet of my code that sends the F1 key:

// DLL Imports

//Set the active window
[DllImport("user32.dll")]
public static extern IntPtr SetActiveWindow(IntPtr hWnd);

//sends a windows message to the specified window
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

// ...

// Some constants
#define WM_SYSKEYDOWN 260
#define WM_SYSKEYUP 261
#define WM_CHAR 258
#define WM_KEYDOWN 256
#define WM_KEYUP 257

// ...

// activate the window and send F1
SetActiveWindow(hWnd);
ushort action = (ushort)WM_SYSKEYDOWN;
ushort key = (ushort)System.Windows.Forms.Keys.F1;
SendMessage(hWnd, action, key, 0);

One interesting side note is that even though the above code works in sending the F1 key to the target application it is not the same as what I see using Spy++. Here is the output of the Spy++ log whenever I hit the F1 key while monitoring the target application:

<00001> 00050412 P WM_KEYDOWN nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00002> 00050412 P WM_KEYUP nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:0 fRepeat:1 fUp:1

Note that there are two messages sent, WM_KEYDOWN and WM_KEYUP.

My first question would be, why am I succesful sending F1 using WM_SYSKEYDOWN when Spy++ tells me that WM_KEYDOWN + WM_KEYUP is the proper message sequence?

Moving on to my next challenge of trying to send ALT + F1.

I have used Spy++ to monitor the messages passed when pressing ALT + F1 on my keyboard and this is what I see:

<00001> 00050412 P WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:1 fAltDown:1 fRepeat:0 fUp:0
<00002> 00050412 P WM_SYSKEYDOWN nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:1 fRepeat:0 fUp:0
<00003> 00050412 P WM_SYSKEYUP nVirtKey:VK_F1 cRepeat:1 ScanCode:3B fExtended:0 fAltDown:1 fRepeat:1 fUp:1
<00004> 00050412 P WM_KEYUP nVirtKey:VK_MENU cRepeat:1 ScanCode:38 fExtended:1 fAltDown:0 fRepeat:1 fUp:1

Given the above Spy++ message capture I tried to send the exact message sequence using the following code (simplified):

SetActiveWindow(hWnd);    
SendMessage(hWnd, (ushort)WM_SYSKEYDOWN, (ushort)System.Windows.Forms.Keys.Menu, 0);
SendMessage(hWnd, (ushort)WM_SYSKEYDOWN, (ushort)System.Windows.Forms.Keys.F1, 0);
SendMessage(hWnd, (ushort)WM_SYSKEYUP, (ushort)System.Windows.Forms.Keys.F1, 0);
SendMessage(hWnd, (ushort)WM_KEYUP, (ushort)System.Windows.Forms.Keys.Menu, 0);

This did not work.

And so this leads to my next question. Is there anything else I can try or is there something that I am doing wrong here?

Whenever I captured the output of my program using Spy++ here is what was logged:

<00001> 00050412 S WM_SYSKEYDOWN nVirtKey:VK_MENU cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00002> 00050412 R WM_SYSKEYDOWN
<00003> 00050412 S WM_SYSKEYDOWN nVirtKey:VK_F1 cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00004> 00050412 R WM_SYSKEYDOWN
<00005> 00050412 S WM_SYSKEYUP nVirtKey:VK_F1 cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00006> 00050412 R WM_SYSKEYUP
<00007> 00050412 S WM_KEYUP nVirtKey:VK_MENU cRepeat:0 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 fUp:0
<00008> 00050412 R WM_KEYUP

Notice that there are extra messages being sent at lines 2, 4, 6, and 8. Could this be the reason why things are not working?

I have one final question regarding the difference between the messages captured from actual keyboard input and those captured using my application. Notice the cRepeat, ScanCode, fExtended, etc. arguments. They are non-zero in the messages that were
captured using my keyboard as the input and they are all zero in the messages sent by my application. Could this be the reason why my code is not working? If so, how do I modify these values? (I am assuming they come from the 4th argument to the SendMessage
function, which I have set to zero in all cases.)

Thank you,

Jan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:16:07+00:00Added an answer on June 15, 2026 at 7:16 am

    Another solution. It doesn’t appear you are passing anything for the lparam of the WM_SYSKEYDOWN. Yet the docs, clearly suggest that bit 29 of the lparam needs to be set to indicate the ALT key was pressed.

    ushort action = (ushort)WM_SYSKEYDOWN;
    ushort key = (ushort)System.Windows.Forms.Keys.F1;
    uint lparam = (0x01 << 28);
    SendMessage(hWnd, action, key, lparam);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Using SendMessage to enter text into an edit control belonging to another
A possible duplicate: Directly sending keystrokes to another process via hooking I would like
Possible Duplicate: Filetype association with application (C#) I'm writing a C# Windows app to
Possible Duplicate: Difference of create Index by using include column or not using Edit:
Possible Duplicate: Rownum not working with query I am running some tests for how
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: how to delete a file? My application first reads the fields in
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: What is 0x10 in decimal? I notice that Console.WriteLine(18); writes 18, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.