C# 当按键被按下时发出通知
您好,我正在编写一个程序,它会注意到何时按下特定的键并在该人正在写的地方写下特定的字母。 但我有一个问题,那就是我不想需要将该程序作为“制作的程序”。相反,我确实希望它在用户按下按键时触发并写出用户正在写入的特定字母或文本...
我希望您理解并感谢所有帮助,
这是我到目前为止得到的代码:
static void Main(string[] args)
{
while (true)
{
ConsoleKeyInfo cki;
cki = Console.ReadKey();
Console.WriteLine(cki.Key.ToString());
if (cki.Key.ToString() == "A" && (cki.Modifiers & ConsoleModifiers.Control) != 0)
{
System.Threading.Thread.Sleep(400);
TSendKeys.SendWait("ø");
}
}
}
Hello I am doing a program that will notice when a specific key is pressed and write an specific letter where the person is writing.
But i have a problem, it is that i not want to be needing to have the program as the "maked one". instead I do want it to trigger and write out a spesifick letter or text where the user is writing as the user is pressing the key...
I hope you understand and thanks for all help
here's the code i get so far:
static void Main(string[] args)
{
while (true)
{
ConsoleKeyInfo cki;
cki = Console.ReadKey();
Console.WriteLine(cki.Key.ToString());
if (cki.Key.ToString() == "A" && (cki.Modifiers & ConsoleModifiers.Control) != 0)
{
System.Threading.Thread.Sleep(400);
TSendKeys.SendWait("ø");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
读这篇文章我认为您正在谈论 Windows 挂钩。
这意味着用户在另一个应用程序(例如
Word
)中打印某些内容,但您可以捕获该键(即使您的应用程序中没有按下该键)。如果这是您真正想要的,这个 CodeProject 文章就是您所需要的。Reading this I think you're talking about Windows Hooks.
This means that user prints something in another application, let's say
Word
, but you're able to catch that key (even if it wasn't pressed in your app). If this is what you actually want, this CodeProject article is what you need.您可能需要使用钩子,本文 详细解释了如何执行此操作
You will probably have to use a hook, this article explains how to do this in detail
一种方法是模拟 Console.ReadLine 的行为,而实际上分别读取和输出每个字符。当读取特殊序列时,例如CTRL+A,可以拦截该行为并插入字符。基本实现如下。
驱动:
输入行时,按 CTRL+A 将插入“^”字符。箭头键、Escape、Home 和 End 的功能应与 Console.ReadLine 的正常功能相同。 (为了简化代码,自定义插入逻辑是硬连线的。)
One approach is to emulate the behavior of Console.ReadLine, while in actuality reading and outputting each character separately. When reading special sequences, such as CTRL+A, the behavior can be intercepted and character inserted. A basic implementation is below.
To drive:
When entering the line, pressing CTRL+A will insert the "^" character. Arrow keys, Escape, Home, and End should function as they do normally for Console.ReadLine. (To simplify the code, the custom insertion logic is hardwired in.)