我用c#Windows Forms制作了一个屏幕键盘。我使用Sendkeys.Send()
函数发送击键。所有字母,但字母 i 工作正常。当我在Microsoft Word打开时按下键盘上的字母 i 时,它会发送 Ctrl + Alt + I 并打开打印对话框。在Notepad ++中也是如此。但是当我尝试输入记事本时,它工作正常。
在我的代码中,我发送了SendKeys.Send(value);
的键,其中value是按下的按钮的文本。我使用以下代码获取文本:
string s = ((Button)sender).Text;
关于它无法正常工作的任何评论?
编辑:我用一个按钮创建了一个新的Windows窗体项目,整个代码如下。还是行不通。任何想法都将不胜感激。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("i");
}
// Prevent form being focused
const int WS_EX_NOACTIVATE = 0x8000000;
protected override CreateParams CreateParams
{
get
{
CreateParams ret = base.CreateParams;
ret.ExStyle |= WS_EX_NOACTIVATE;
return ret;
}
}
}
重写的功能是防止表单被聚焦。这样我就可以将键击发送到另一个具有焦点的应用程序。
答案 0 :(得分:1)
两种选择:
1- 模拟按键,请参阅http://msdn2.microsoft.com/en-us/library/system.windows.forms.sendkeys(VS.71).aspx
样品:
public static void ManagedSendKeys(string keys)
{
SendKeys.SendWait(keys);
SendKeys.Flush();
}
2-将按键发送到窗口,按下按钮x秒
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
public static void KeyboardEvent(Keys key, IntPtr windowHandler, int delay)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
Thread.Sleep(delay);
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
}
答案 1 :(得分:0)
您没有调用“SetForegroundWindow”Win32 API方法。因此,您的“SendKeys”通话可能会将密钥发送到您的应用,而不是目标应用。
以下是MSDN上的一个示例:
How to: Simulate Mouse and Keyboard Events in Code
此外,这是示例中的代码:
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
namespace SimulateKeyPress
{
class Form1 : Form
{
private Button button1 = new Button();
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1()
{
button1.Location = new Point(10, 10);
button1.TabIndex = 0;
button1.Text = "Click to automate Calculator";
button1.AutoSize = true;
button1.Click += new EventHandler(button1_Click);
this.DoubleClick += new EventHandler(Form1_DoubleClick);
this.Controls.Add(button1);
}
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
// Send a series of key presses to the Calculator application.
private void button1_Click(object sender, EventArgs e)
{
// Get a handle to the Calculator application. The window class
// and window name were obtained using the Spy++ tool.
IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");
// Verify that Calculator is a running process.
if (calculatorHandle == IntPtr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
// Make Calculator the foreground application and send it
// a set of calculations.
SetForegroundWindow(calculatorHandle);
SendKeys.SendWait("111");
SendKeys.SendWait("*");
SendKeys.SendWait("11");
SendKeys.SendWait("=");
}
// Send a key to the button when the user double-clicks anywhere
// on the form.
private void Form1_DoubleClick(object sender, EventArgs e)
{
// Send the enter key to the button, which raises the click
// event for the button. This works because the tab stop of
// the button is 0.
SendKeys.Send("{ENTER}");
}
}
}