【问题标题】:How do I compile a program that can repeat a keystroke while the same key is held?如何编译可以在按住同一个键时重复击键的程序?
【发布时间】:2021-05-11 12:31:25
【问题描述】:

我一直在尝试制作一个简单的程序,可以在按住同一个键的同时重复发送一个键。我尝试在下面使用带有“Windows 窗体”的示例,但我收到错误“找不到类型或命名空间“密钥””,即使我已包含 System.ComponentModel.DataAnnotations,据我所知,密钥应该成为其中的一部分。我以前从未在 Windows 窗体中编写过程序,但据我所知,它基本上包括一个带有您的代码的 GUI?我使用 Windows 窗体的唯一原因是因为我看到的示例使用了它,但如果它可以在常规 C# 程序中完成,那也很棒。

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

namespace spamkey2
{
    public partial class Form1 : Form
    {
        bool keyHold = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (keyHold)
            {
                //Do stuff
            }
        }

        private void Key_up(object sender, KeyEventArgs e)
        {
            Key key = (Key)sender;
            if (key == Key.A) //Specify your key here !
            {
                keyHold = false;
            }
        }

        private void Key_down(object sender, KeyEventArgs e)
        {
            Key key = (Key)sender;
            if (key == Key.A) //Specify your key here !
            {
                keyHold = true;
            }
        }
    }
}

【问题讨论】:

    标签: c# keyboard keyboard-events


    【解决方案1】:

    使用此代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.ComponentModel.DataAnnotations;
    
    namespace spamkey2
    {
        public partial class Form1 : Form
        {
            bool keyHold = false;
            public Form1()
            {
                InitializeComponent();
                this.KeyDown += Key_down;
                this.KeyUp += Key_up;
                System.Timers.Timer timer = new System.Timers.Timer();
                timer.Elapsed += Timer_Elapsed;
                timer.Interval = 100;
                timer.Start();
            }
    
            private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                if (keyHold)
                {
                    //Do stuff
                    Console.WriteLine("Key A is down");
                }
            }
    
            private void Key_up(object sender, KeyEventArgs e)
            {
                if (e.KeyData == Keys.A) //Specify your key here !
                {
                    keyHold = false;
                }
            }
    
            private void Key_down(object sender, KeyEventArgs e)
            {
                if (e.KeyData == Keys.A) //Specify your key here !
                {
                    keyHold = true;
                }
            }
        }
    }
    

    【讨论】:

    • 非常感谢它有效!抱歉,在评论中格式化代码时遇到问题,我把 SendKeys.Send("f");如果密钥被持有,就会发生,但它会给出错误。我应该使用其他功能吗?
    • SendKeys.Send ("f") ;?您将密钥 (e.KeyData == Keys.A) 替换为密钥 (e.KeyData == Keys.F)
    • 此代码 Key key = (Key)sender;是错的 。 (sender) 是为其定义事件的控件。这是表单本身。
    • 我在 if (keyHold) 下添加了它,以使其在按住键时模拟 f 按下,但是是的,代码执行没有错误,谢谢!
    • 你得到你想要的了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2017-02-19
    • 2019-05-19
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    相关资源
    最近更新 更多