求助:C#控制台应用程序中,如何即时地判断用户的键盘按键

zlo309618100 2012-05-29 01:37:33
问题如题。其中的“即时”是说程序可以检测并判断用户所有的键盘按键。

在这先谢过各位的解答。



如果我的解释不够清晰的话,那么您可以看一下下面这个例子:



有这样一个简单的题目:从键盘输入一串字符,编写程序,去掉其中重复的字符(保留重复字符的第一个),然后将结果显示在屏幕中。

这个题目的基本要求实现之后,我想让用户可以重复进行这一操作:输入一个字符串,然后输出处理后的字符串;然后继续输入……而不需要再次运行程序。

那么可以把用户输入、处理方法和输出的代码放到一个循环中,循环的条件是某个键盘按键……但这种解决方法的局限是,用户决定是否继续的决定权是受限制的,即用户必须在输入、处理和输出这三个步骤结束之后才能决定是否继续。下面给出代码,可能会便于说明:

1 class Program
2 {
3 static void Main(string[] args)
4 {
5 string strInput = string.Empty;
6
7 do
8 {
9 Console.WriteLine("\nPlease input a string:");
10 strInput = Console.ReadLine();
11 strInput = RemoveRepeatedLetter(strInput);
12 Console.WriteLine("result string:\n" + strInput);
13 } while (ContinueOrNot());
14 }
15 public static string RemoveRepeatedLetter(string strOriginal)
16 {
17 string strResult = string.Empty;
18
19 foreach (char c in strOriginal)
20 {
21 if (!strResult.Contains(c.ToString()))
22 {
23 strResult += c.ToString();
24 }
25 }
26
27 return strResult;
28 }
29 public static bool ContinueOrNot()
30 {
31 ConsoleKey c = ConsoleKey.Escape;
32
33 Console.Write("Continue, Y/N? ");
34 c = Console.ReadKey(false).Key;
35 if (c.CompareTo(ConsoleKey.N) == 0)
36 {
37 return false;
38 }
39 else
40 {
41 return true;
42 }
43 }
44 }
而与本文题目对应的,我真正想要实现的是用户可以随时结束,比如在输入字符串的过程中——例如,上述代码中的循环条件是“按键不是n/N”——当用户按下n/N键之后,程序立即结束运行。
请问怎么办呢?可不可以注册事件进行捕获
...全文
2979 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hard9999 2012-05-29
  • 打赏
  • 举报
回复
你可以创建一个winform不显示,捕获keydown事件。
也可以用下面这个方法:(声明:网上拷贝的)
必须使用Windows api,用键盘钩子函数截取键盘按键记录,然后把这个EXE程序注册为系统服务就能自动运行了,C#中键盘钩子的使用
public class Win32Hook
{

[DllImport("kernel32")]
public static extern int GetCurrentThreadId();

[DllImport( "user32",
CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(
HookType idHook,
HOOKPROC lpfn,
int hmod,
int dwThreadId);

public enum HookType
{
WH_KEYBOARD = 2
}

public delegate int HOOKPROC(int nCode, int wParam, int lParam);

public void SetHook()
{
// set the keyboard hook
SetWindowsHookEx(HookType.WH_KEYBOARD,
new HOOKPROC(this.MyKeyboardProc),
0,
GetCurrentThreadId());
}

public int MyKeyboardProc(int nCode, int wParam, int lParam)
{
//在这里放置你的处理代码 return 0;
}
}
使用方法
可以在Form的构造函数里放入
Win32Hook hook = new Win32Hook();
hook.SetHook();
threenewbee 2012-05-29
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string output = "";
char input = '\0';
while (true)
{
input = Console.ReadKey().KeyChar;
if (input == 'q' || input == 'Q') break;
if ((int)input == 8 && output.Length > 0)
output = output.Substring(0, output.Length - 1);
else if ((int)input == 13)
{
output = new string(output.GroupBy(x => x.ToString().ToLower()).Select(x => x.First()).ToArray());

}
else
output += input;
Console.Clear();
Console.Write(output);
}
}
}
}

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧