日期:2014-05-17  浏览次数:21184 次

c# 如何获取键盘输入值?
 
  我的思路是: 在应用程序中创建一个txt文本; 通过”按键“记录键盘按键的值;
   
  句柄是一个什么概念?

------解决方案--------------------
C# code
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    MessageBox.Show(((int)(e.KeyChar)).ToString());
}

------解决方案--------------------
IntPtr hwnd = FindWindow(null, "a.txt"); 
if (hwnd.ToInt32() != 0)
{
if (SetForegroundWindow(hwnd))
SendKeys.SendWait(Keys.A.ToString()); 
}
else { MessageBox.Show("记事本没有运行 "); } 
Process myProcess = System.Diagnostics.Process.Start(@"D:\WINDOWS\notepad.exe");
 myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
 myProcess.WaitForInputIdle(1000);
 if (myProcess.Responding)
 {
SendKeys.SendWait("文字!");
SendKeys.SendWait("{ENTER}");
SendKeys.SendWait("%EA");
SendKeys.SendWait("^C");
SendKeys.SendWait("^V");
SendKeys.SendWait("%FS");
SendKeys.SendWait(@"C:\Test.txt");
SendKeys.SendWait("%S");
 }
private void Form1_KeyUp ( object sender , KeyEventArgs e ) 

MessageBox.Show(e.KeyCode.ToString ( )) ; 

句柄是一个32位的整数,是Windows在内存中维护的一个对象(窗口等)内存物理地址列表的整数索引