我正在尝试在C#中创建一个类似于AHK的热键功能。就像在任何电子游戏中一样,你点击一个方框,按下你的热键,然后注册它。这就是我要用textBox做的事情:
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的基本功能,但我不知道如何关闭。例如:光标仍然处于活动状态,我可以突出显示其中的文本。
发布于 2018-09-25 15:26:52
您可以将文本框的属性Enabled设置为false,然后将背景颜色设置为白色,字体设置为黑色,这样看起来就不会显示为禁用状态,但您既不能聚焦也不能单击ir。
textBox1.Enabled = false;顺便说一下,我有一个非常简单的库,可能会很有用。
https://github.com/PabloHorno/HotKeyDialog
一旦你引用了这个库,你可以像这样使用它,或者以一种更简单的方式使用它
var hotKey = new HotKey();
hotKey = HotKeyMessageBox.Show("Title", "A little description");
hotKey.ToString();或者,您可以检查用户是否关闭了该框
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";希望它能帮上忙!
发布于 2017-08-03 13:08:45
如果您不需要TextBox的功能,为什么还要使用它?
您可以创建一个简单的自定义控件并将其放在窗体上,而不是关闭它的功能。类似于:
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);
}
}https://stackoverflow.com/questions/45473186
复制相似问题