简体   繁体   English

单击按钮,将单击Key

[英]Click on button and that will click on Key

In C# I want to have a button clicked. 在C#中,我希望点击一个按钮。 I don't want to use a button event like button.click+=... I want to click on the button in Form Application, and any key of my computer will be clicked. 我不想使用button.click + =这样的按钮事件...我想点击表格应用程序中的按钮,我的计算机的任何键都会被点击。 I tried SendKeys but it doesn't work well. 我试过SendKeys,但效果不好。

I need more ideas how to do it. 我需要更多的想法如何做到这一点。 I tried: 我试过了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
    Button[,] b;
    List<char> chrlist = new List<char>();
    public Form5()
    {
        InitializeComponent();
        start();
    }

    public void start()
    {
        panel1.Controls.Clear();
        int m = 13;
        int n = 2;
        b = new Button[n, m];
        int x = 0;
        int i, j;
        int y = 0;
        // int count = 0;
        int chr1=(int)'A';
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {
                //  count++;
                b[i, j] = new Button();
                b[i, j].SetBounds(x, y, panel1.Size.Width / m, panel1.Size.Height / n);
                b[i, j].Name = i + "," + j;
                b[i, j].Text = ((char)chr1).ToString();
                   b[i, j].Click += new EventHandler(ButtonClick);
                panel1.Controls.Add(b[i, j]);
                x = x + panel1.Size.Width / m;
                chr1++;
            }
            y = y + panel1.Size.Height / n;
            x = 0;
        }
    }

    public void ButtonClick(Object sender, EventArgs e)
    {
        Button a = (Button)sender;
        MessageBox.Show(a.Text);

    }
    public void started()
    {
        while (true)
        {
            int sec = 1000;
            Thread.Sleep(sec * 3);
            SendKeys.SendWait("{TAB}");
        }

    }


    private void button1_Click(object sender, EventArgs e)
    {
        Thread workerThread = new Thread(started);
        workerThread.Start();

    }
}

} }

And that didn't work in my other application. 这在我的其他应用程序中不起作用。

It looks to me like you're attempting to build an onscreen keyboard . 在我看来,你正试图建立一个屏幕键盘

Premise : Use SendKeys to send a letter to the active application. 前提 :使用SendKeys向活动应用程序发送一封信。

Question : When you click the button in your Form to send a letter, what application has focus? 问题 :当您单击表单中的按钮发送信件时,哪些应用程序具有焦点?

Answer : Yours. :你的。 Therefore the key will be sent to your application, not the last application that was focused. 因此,密钥将被发送到您的应用程序,而不是最后关注的应用程序。

Solution : Prevent your application from getting focused. 解决方案 :防止您的应用程序集中注意力。

This can be accomplished by setting the WS_EX_NOACTIVATE flag in your extended window styles via CreateParams() 这可以通过CreateParams()在扩展窗口样式中设置WS_EX_NOACTIVATE标志来实现。

    private const int WS_EX_NOACTIVATE = 0x08000000;

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams p = base.CreateParams;
            p.ExStyle |= WS_EX_NOACTIVATE;
            return p;
        }
    }

    public void ButtonClick(Object sender, EventArgs e)
    {
        SendKeys.Send(((Control)sender).Text);
    }

Now when you click a button in your OSK, the currently focused application will STAY focused (because your application cannot receive focus) and SendKeys() will correctly target it. 现在,当您单击OSK中的按钮时,当前关注的应用程序将保持聚焦状态(因为您的应用程序无法获得焦点),SendKeys()将正确定位它。

*Caveats : This only works if the onscreen keyboard is a different application. *注意事项 :仅当屏幕键盘是不同的应用程序时才有效。 In other words, the OSK cannot target other forms in your own application...it's a weird focus thing. 换句话说,OSK无法在您自己的应用程序中定位其他表单......这是一个奇怪的焦点。 To target your own app you'd have to manually track the last form in your application and give it focus again before sending the keys. 要定位您自己的应用,您必须手动跟踪应用中的最后一个表单,并在发送密钥之前再次关注它。

Here's your OSK sending keys to Notepad: 这是你的OSK向记事本发送密钥: 屏幕键盘示例

This is ALL of the code in my test app: 这是我的测试应用程序中的所有代码:

public partial class Form1 : Form
{
    private Button[,] b;

    private const int WS_EX_NOACTIVATE = 0x08000000;

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams p = base.CreateParams;
            p.ExStyle |= WS_EX_NOACTIVATE;
            return p;
        }
    }

    public Form1()
    {
        InitializeComponent();
        start();
    }

    public void start()
    {
        panel1.Controls.Clear();
        int m = 13;
        int n = 2;
        b = new Button[n, m];
        int x = 0;
        int i, j;
        int y = 0;
        // int count = 0;
        int chr1 = (int)'A';
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {
                //  count++;
                b[i, j] = new Button();
                b[i, j].SetBounds(x, y, panel1.Size.Width / m, panel1.Size.Height / n);
                b[i, j].Name = i + "," + j;
                b[i, j].Text = ((char)chr1).ToString();
                b[i, j].Click += new EventHandler(ButtonClick);
                panel1.Controls.Add(b[i, j]);
                x = x + panel1.Size.Width / m;
                chr1++;
            }
            y = y + panel1.Size.Height / n;
            x = 0;
        }
    }

    public void ButtonClick(Object sender, EventArgs e)
    {
        SendKeys.Send(((Control)sender).Text);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM