Sending keys to inactive application in C#/.NET [duplicate]

心已入冬 提交于 2019-12-20 03:22:46

问题


I have an application with combobox that contains names of currently running applications. As I understood from msdn library, SendKeys method can send keys only to active application. Is it somehow possible in .NET, to send keys also to inactive app? Or at least in WinAPI ?


回答1:


You can use the SendMessage() API function to send keystrokes to an inactive window.




回答2:


With C# <3 is everthing possible :D

No need to be active window, as u wished.

Also here a usefull list of Virtual Key Codes

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);

        private void button1_Click(object sender, EventArgs e)
        {
            const int WM_SYSKEYDOWN = 0x0104;
            const int VK_KEY_A = 0x41;

            IntPtr WindowToFind = FindWindow(null, "Window Name");

          //In ur case u have to write a code that translates the combobox into Virtual Key Codes. Will take time but it shouls be easy

            PostMessage(WindowToFind, WM_SYSKEYDOWN, VK_KEY_A, 0);
          //PostMessage(WindowToFind, WM_SYSKEYDOWN, ((int)Keys.NumPad7), 0);
        }


来源:https://stackoverflow.com/questions/1584767/sending-keys-to-inactive-application-in-c-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!