首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#热键盒(AHK热键式)

C#热键盒(AHK热键式)
EN

Stack Overflow用户
提问于 2017-08-03 09:02:42
回答 2查看 197关注 0票数 1

我正在尝试在C#中创建一个类似于AHK的热键功能。就像在任何电子游戏中一样,你点击一个方框,按下你的热键,然后注册它。这就是我要用textBox做的事情:

代码语言:javascript
复制
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;

namespace Keybinder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            KeyPreview = true;
            textBox1.ReadOnly = true;
            textBox1.Focus();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = "HELLO";
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char key = e.KeyChar;
            string keystring = Char.ToString(key);
            textBox1.Text = keystring;
        }
    }
}

然而,问题是,我需要关闭textBox的基本功能,但我不知道如何关闭。例如:光标仍然处于活动状态,我可以突出显示其中的文本。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-25 15:26:52

您可以将文本框的属性Enabled设置为false,然后将背景颜色设置为白色,字体设置为黑色,这样看起来就不会显示为禁用状态,但您既不能聚焦也不能单击ir。

代码语言:javascript
复制
textBox1.Enabled = false;

顺便说一下,我有一个非常简单的库,可能会很有用。

https://github.com/PabloHorno/HotKeyDialog

一旦你引用了这个库,你可以像这样使用它,或者以一种更简单的方式使用它

代码语言:javascript
复制
var hotKey = new HotKey();

hotKey = HotKeyMessageBox.Show("Title", "A little description");

hotKey.ToString();

或者,您可以检查用户是否关闭了该框

代码语言:javascript
复制
 HotKey hotkey = new HotKey();

 if (HotKeyMessageBox.Show("Title", "Please press the key combination", out hotkey) == DialogResult.OK)
     label.Text = "You have pressed the keys: " + hotkey.ToString();
 else
     label.Text = "You have closed the dialog. There is no input";

希望它能帮上忙!

票数 1
EN

Stack Overflow用户

发布于 2017-08-03 13:08:45

如果您不需要TextBox的功能,为什么还要使用它?

您可以创建一个简单的自定义控件并将其放在窗体上,而不是关闭它的功能。类似于:

代码语言:javascript
复制
public class KeyInput : UserControl
{
    public string KeyString { get; set; } = "HELLO";

    public KeyInput() : base()
    {
        BorderStyle = BorderStyle.Fixed3D;
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);

        KeyString = e.KeyChar.ToString();
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        e.Graphics.DrawString(KeyString, Font, SystemBrushes.ControlText, 0, 0);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45473186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档