Solved

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

Posted on 2009-07-07
13
2,288 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#?
0
Comment
Question by:pratikkum
[X]
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
13 Comments
 
LVL 7

Expert Comment

by:Seo_Expert
ID: 24801285
0
 

Author Comment

by:pratikkum
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??
0
 

Author Comment

by:pratikkum
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??
0
Independent Software Vendors: 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

by:
Agarici earned 500 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


hth,
A.
0
 

Author Comment

by:pratikkum
ID: 24840292
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
0
 

Author Comment

by:pratikkum
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.
0
 
LVL 11

Expert Comment

by:Agarici
ID: 24840495
what exactly do you want to send?
0
 

Author Comment

by:pratikkum
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()
        {
            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);
                }
            }
        }
    }
}
0
 

Author Closing Comment

by:pratikkum
ID: 31600976
It was a good help.Thanks a lot
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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