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

Posted on 2009-07-07
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
  • 6
  • 2

Expert Comment

Comment Utility

Author Comment

Comment Utility
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

Comment Utility
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??
LVL 11

Accepted Solution

Agarici earned 500 total points
Comment Utility
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:

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.


Author Comment

Comment Utility
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

Comment Utility
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

Comment Utility
what exactly do you want to send?

Author Comment

Comment Utility
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

Comment Utility
It was a good help.Thanks a lot

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now