We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

Medium Priority
4,372 Views
Last Modified: 2013-12-17
Hi
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#?
Comment
Watch Question

Author

Commented:
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

Commented:
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??
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Virtual Key Codes
Symbolic constant name      Value (hexadecimal)      Mouse or keyboard
equivalent
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

Commented:
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.

Commented:
what exactly do you want to send?

Author

Commented:
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()
        {
            InitializeComponent();
        }

        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

        [DllImport("user32.dll")]
        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);

        [DllImport("user32.dll")]
        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

Commented:
It was a good help.Thanks a lot
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.