[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


how to send key strokes to a minimized window in C#

Posted on 2009-07-07
Medium Priority
Last Modified: 2013-12-17
I want to send key strokes to a window, let's say notepad, when it is minimized or hidden.
Is it possible to do in C# ? if yes how to do it in C#?
Question by:pratikkum
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2

Expert Comment

ID: 24801285

Author Comment

ID: 24801959
see ,, what i'm doing is :

1) i'm opening a notepad in minimized mode using c#.
2) i'm sending key strokes to notepad lets say ^v using sendkeys function of C# which I read on msdn help

but,it's not working ,,probably because the notepad is not active or not in focus.So, what to do for this??

Author Comment

ID: 24811078
well I need it quite urgently actually.So,it wud be nice if somebody looks into it as soon as possible.
Secondly, I don't understand why this question is moved to "neglected question" category??
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Accepted Solution

Agarici earned 1500 total points
ID: 24837884
you should be able to do this by either doing SendMessage or PostMessage to the minimized window.
that is:
you should get the minimized window handle (a HWND, translated to an IntPtr in C#)
then call either SerndMessage or PostMessage with the hwnd obtained above as the first param and WM_KeyDown/WM_KeyUp as second param.
the third of the params have to be build in a more complex way.
see these links to get more details:
(sendMessage) http://msdn.microsoft.com/en-us/library/ms644950%28VS.85%29.aspx
(WM_KEYDOWN) http://msdn.microsoft.com/en-us/library/ms646280(VS.85).aspx
(WM_KEYUP) http://msdn.microsoft.com/en-us/library/ms646281(VS.85).aspx


Author Comment

ID: 24840292
Virtual Key Codes
Symbolic constant name      Value (hexadecimal)      Mouse or keyboard
VK_LBUTTON      01      Left mouse button
VK_RBUTTON      02      Right mouse button
VK_CANCEL      03      Control-break processing
VK_MBUTTON      04      Middle mouse button (three-button mouse)
¾ 05-07      Undefined
VK_BACK      08      BACKSPACE key
VK_TAB      09      TAB key
¾ 0A-0B      Undefined
VK_CLEAR      0C      CLEAR key
VK_RETURN      0D      ENTER key
¾ 0E-0F      Undefined
VK_SHIFT      10      SHIFT key
VK_CONTROL      11      CTRL key
VK_MENU      12      ALT key
VK_PAUSE      13      PAUSE key
VK_CAPITAL      14      CAPS LOCK key
¾ 15-19      Reserved for Kanji systems
¾ 1A      Undefined
VK_ESCAPE      1B      ESC key
¾ 1C-1F      Reserved for Kanji systems
VK_SPACE      20      SPACEBAR
VK_PRIOR      21      PAGE UP key
VK_NEXT      22      PAGE DOWN key
VK_END      23      END key
VK_HOME      24      HOME key
VK_LEFT      25      LEFT ARROW key
VK_UP      26      UP ARROW key
VK_RIGHT      27      RIGHT ARROW key
VK_DOWN      28      DOWN ARROW key
VK_SELECT      29      SELECT key
¾ 2A      Original equipment manufacturer (OEM) specific
VK_EXECUTE      2B      EXECUTE key
VK_SNAPSHOT      2C      PRINT SCREEN key for Windows 3.0 and later
VK_INSERT      2D      INS key
VK_DELETE      2E      DEL key
VK_HELP      2F      HELP key
VK_0      30      0 key
VK_1      31      1 key
VK_2      32      2 key
VK_3      33      3 key
VK_4      34      4 key
VK_5      35      5 key
VK_6      36      6 key
VK_7      37      7 key
VK_8      38      8 key
VK_9      39      9 key
¾ 3A-40      Undefined
VK_A      41      A key
VK_B      42      B key
VK_C      43      C key
VK_D      44      D key
VK_E      45      E key
VK_F      46      F key
VK_G      47      G key
VK_H      48      H key
VK_I      49      I key
VK_J      4A      J key
VK_K      4B      K key
VK_L      4C      L key
VK_M      4D      M key
VK_N      4E      N key
VK_O      4F      O key
VK_P      50      P key
VK_Q      51      Q key
VK_R      52      R key
VK_S      53      S key
VK_T      54      T key
VK_U      55      U key
VK_V      56      V key
VK_W      57      W key
VK_X      58      X key
VK_Y      59      Y key
VK_Z      5A      Z key
VK_LWIN      5B      Left Windows key (Microsoft Natural Keyboard)
VK_RWIN      5C      Right Windows key (Microsoft Natural Keyboard)
VK_APPS      5D      Applications key (Microsoft Natural Keyboard)
¾ 5E-5F      Undefined
VK_NUMPAD0      60      Numeric keypad 0 key
VK_NUMPAD1      61      Numeric keypad 1 key
VK_NUMPAD2      62      Numeric keypad 2 key
VK_NUMPAD3      63      Numeric keypad 3 key
VK_NUMPAD4      64      Numeric keypad 4 key
VK_NUMPAD5      65      Numeric keypad 5 key
VK_NUMPAD6      66      Numeric keypad 6 key
VK_NUMPAD7      67      Numeric keypad 7 key
VK_NUMPAD8      68      Numeric keypad 8 key
VK_NUMPAD9      69      Numeric keypad 9 key
VK_MULTIPLY      6A      Multiply key
VK_ADD      6B      Add key
VK_SEPARATOR      6C      Separator key
VK_SUBTRACT      6D      Subtract key
VK_DECIMAL      6E      Decimal key
VK_DIVIDE      6F      Divide key
VK_F1      70      F1 key
VK_F2      71      F2 key
VK_F3      72      F3 key
VK_F4      73      F4 key
VK_F5      74      F5 key
VK_F6      75      F6 key
VK_F7      76      F7 key
VK_F8      77      F8 key
VK_F9      78      F9 key
VK_F10      79      F10 key
VK_F11      7A      F11 key
VK_F12      7B      F12 key
VK_F13      7C      F13 key
VK_F14      7D      F14 key
VK_F15      7E      F15 key
VK_F16      7F      F16 key
VK_F17      80H      F17 key
VK_F18      81H      F18 key
VK_F19      82H      F19 key
VK_F20      83H      F20 key
VK_F21      84H      F21 key
VK_F22      85H      F22 key
VK_F23      86H      F23 key
VK_F24      87H      F24 key
¾ 88-8F      Unassigned
VK_NUMLOCK      90      NUM LOCK key
VK_SCROLL      91      SCROLL LOCK key
¾ 92-B9      Unassigned
¾ BA-C0      OEM specific
¾ C1-DA      Unassigned
¾ DB-E4      OEM specific
¾ E5      Unassigned
¾ E6      OEM specific
¾ E7-E8      Unassigned
¾ E9-F5      OEM specific
VK_ATTN      F6      Attn key
VK_CRSEL      F7      CrSel key
VK_EXSEL      F8      ExSel key
VK_EREOF      F9      Erase EOF key
VK_PLAY      FA      Play key
VK_ZOOM      FB      Zoom key
VK_NONAME      FC      Reserved for future use.
VK_PA1      FD      PA1 key
VK_OEM_CLEAR      FE      Clear key

Author Comment

ID: 24840310
LRESULT SendMessage(
    HWND hWnd,          // Handle of destination window
    UINT Msg,           // Message to send
    WPARAM wParam,      // First message parameter
    LPARAM lParam);     // Second message parameter
The hWnd parameter is the window to which the message is sent. The Msg parameter is the message numberwhich is usually a constant such as LB_FINDSTRING or WM_COPY. The WPARAM and LPARAM types are actually aliases for 32-bit integers (Long in Visual Basic). The wParam and lParam parameters differ for each message, as does the return value; you must look up the specific message to see what they mean. Often, wParam or the return value is ignored.

The lParam parameter is particularly interesting. It is a 32-bit integer, which happens to be the size of a pointer, and this value is often used to pass a pointer to a string or a UDT. In other words, lParam is typeless. In Visual Basic, that means that the SendMessage declaration contains the As Any type. Alternatively, you can define several type-safe declarations with different aliases.
LVL 11

Expert Comment

ID: 24840495
what exactly do you want to send?

Author Comment

ID: 24901330
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace test
    public partial class Form1 : Form
        public Form1()

        const int WM_KEYDOWN = 0x100;
        const int WM_KEYUP = 0x101;
        const int WM_SYSKEYDOWN = 0x104;
        const int VK_BACK = 0x08;
        const int VK_F5 = 116; //Ox74

        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        static extern byte VkKeyScan(char ch);

        [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
        static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);

        private void Form1_Load(object sender, EventArgs e)


        private void button1_Click_1(object sender, EventArgs e)
            string msg = "123456";
            IntPtr noteWnd = FindWindow("Notepad", null);
            if (!noteWnd.Equals(IntPtr.Zero))
                IntPtr editWnd = FindWindowEx(noteWnd, IntPtr.Zero, "Edit", null);
                if (!editWnd.Equals(IntPtr.Zero))
                    for (int i = 0; i < msg.Length; i++)
                        PostMessage(editWnd, WM_KEYDOWN, VkKeyScan(msg[i]), 0);
                    PostMessage(editWnd, WM_KEYDOWN, VK_BACK, 0);
                    PostMessage(editWnd, WM_KEYDOWN, VK_BACK, 0);
                    //PostMessage(editWnd, WM_SYSKEYDOWN, VkKeyScan('F'), 1 << 29);
                    PostMessage(editWnd, WM_KEYDOWN, VK_F5, 0);
                    PostMessage(editWnd, WM_KEYUP, VK_F5, 0);
                    //PostMessage(editWnd, WM_KEYUP, VK_F, 0);

Author Closing Comment

ID: 31600976
It was a good help.Thanks a lot

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question