Sendkeys每次都无法正常工作,大多数情况下会失败 [英] Sendkeys is not working everytime, most time it fails

查看:261
本文介绍了Sendkeys每次都无法正常工作,大多数情况下会失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我正在尝试将一些按键发送到另一个正在运行的应用程序.我以记事本为例.但是有时它会起作用,但大多数时候却不起作用.以下是我正在使用的代码.请任何人找出代码中的错误.如果无法纠正,请重定向到其他一些将击键发送到其他应用程序的好方法.

在此先感谢
伊斯梅尔

这是代码:
////////////////////////

Hi all,
I am trying to send some key strokes to another running application after activating it. I used notepad for example. But sometimes it works and most of times not. Following is the code I am using. Please anybody findout what is wrong in the code. If it can not be corrected please redirect me to some other nice ways to send keystrokes to other applications.

Thanks in advance
Ismail

Here is the code:
//////////////////////////

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 send
{
    public partial class Form1 : Form
    {

        [DllImportAttribute("User32.dll")]
        private static extern int FindWindow(String ClassName, String WindowName);


        [DllImportAttribute("User32.dll")]
        private static extern IntPtr SetForegroundWindow(int hWnd);

        [DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);


        public Form1()
        {
            InitializeComponent();
        }


        private void button2_Click(object sender, EventArgs e)
        {

            int hWnd = FindWindow(null, "New Text Document.txt - Notepad");
            if (hWnd > 0)
            {
                SetForegroundWindow(hWnd);
                SendKeys.Send("{TAB}");

                SendKeys.Send("{TAB}");
                SendKeys.Send("TABBBBBBBBBBBBBBB");
            }
            else
            {
                MessageBox.Show("Window Not Found!");
            }


        }


    }
}



//////////////////////



///////////////////////

推荐答案

我使用嗅觉方式与SKYPe进行交互您知道有时它可以工作,而其他人则不行,所以我尝试找到一个可交互SKYpe的库,并且很好用,
如果您的想法不可行,您可以先尝试我尝试过的方法,然后再将过程设置为高优先级.
希望对您有帮助
i use smeller way to interactive with SKYPe but as you know sometimes it works and others not so i try to find a library to interactive SKYpe and it works fine,
if the idea not works with you, you can try what i tried before you can make the process high priority.
hop it helps


感谢您的即时答复.你能说得更清楚吗?我的意思是带有一些代码片段?真的我不知道图书馆的意思.
我尝试使用Postmessage.它可以很好地处理字符串值,就像"abc"一样.但是我不知道如何使用它发布{TAB}或{ENTER}.有人请帮助.
Thank u for ur immediate reply. Can u make it some more clear? I mean with some code snippets? Really I don''t know what u mean by library.
I tried to use Postmessage. It works fine with string values just like ''abc''. But i don''t know how to post {TAB} or {ENTER} using it. Anybody please help.


:笑:是的,我找到了.我使用了后消息功能.效果很好.

它在主应用程序窗口中可以正常工作,但是我必须将击键发送到该应用程序的子窗口(特别是对话框窗口).我无法处理子窗口.如果有人可以帮助我.
:laugh: Yes, I found it. I used postmessage function. It works fine.

It works fine with main application window, but I have to send keystrokes to childwindow of that application(specifically dialog window). I couldn''t get handle of the child window. If anybody can please help me.


这篇关于Sendkeys每次都无法正常工作,大多数情况下会失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆