SendKeys.Send("^v");只有第一次成功?

imissherso 2010-10-12 09:12:12
请看ProcessHotkey函数,本意是每次按下对应热键时模拟发送Ctrl+V,在当前光标处粘贴,可是发现只有第一次有效,跟踪发现每次按下热键都会到这个函数,可是后来的SendKeys.Send("^v");貌似就没用了,换成MessageBox.Show("Here am I");却每次都会成功;什么问题~~?
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, Keys vk);

[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey( IntPtr hWnd, int id );

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(Handle, 888, 3, Keys.D1);
}

protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey();//调用主处理程序
break;
}
base.WndProc(ref m);
}

private void ProcessHotkey()
{
//Clipboard.SetText("Come on Man");
//SendKeys.Send("^v");


MessageBox.Show("Here am I");
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
UnregisterHotKey(Handle, 888);
}
}

...全文
429 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
imissherso 2010-10-12
  • 打赏
  • 举报
回复
The SendKeys class has been updated for the .NET Framework 3.0 to enable its use in applications that run on Windows Vista. The enhanced security of Windows Vista (known as User Account Control or UAC) prevents the previous implementation from working as expected.
The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process.
If your application relies on consistent behavior regardless of the operating system, you can force the SendKeys class to use the new implementation by adding the following application setting to your app.config file.
<appSettings>
<add key="SendKeys" value="SendInput"/>
</appSettings>
To force the SendKeys class to use the previous implementation, use the value "JournalHook" instead.
imissherso 2010-10-12
  • 打赏
  • 举报
回复
楼上能解释清楚点吗?不明白这样做有什么道理~~~
wuyq11 2010-10-12
  • 打赏
  • 举报
回复
添加一个应用程序配置文件,启用新SendKeys类实现。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="SendKeys" value="SendInput"/>
</appSettings>
</configuration>
适合人群:【学习前提】 1. 具备python3语言基础 【学习人群】 1. 手工测试人员学习UI自动化测试技能 2. 开发人员转岗测试开发岗位 3. 如具有丰富的自动化测经验,本课程可能并不满足你学习计划:1.下载课程中配套的软件资料 2.结合课件中Demo可完成学习任务课程目标:掌握UI自动化测试框架的设计与实现课程简介:UI自动化框架的设计需储备基础知识,方能完成自动化测试框架的实现,如数据驱动、日志、配置文件等AutoUiTestFrame自动化框架的目录结构初步进行规划,目录结构如下:Config 配置文件的目录v  config.ini 配置文件;v  globalconfig.py 获得日志路径、测试用例路径、测试报告路径、测试数据路径;v  Data 测试数据;v  TestData.xlsx 测试数据。Public 公共文件库v  Common 封装的公共的方法n  Commonconfig.py 公共的参数配置:调试过程中的测试数据等;n  DoExcel.py 操作excel(数据驱动);n  Send_mail.py 发送邮件(html);n  ReadConfigIni.py 读取ini格式的配置文件;n  TestCaseInfo.py  测试用例信息;n  Log.py 日志类。设置日志类,其他模块或文件需要日志类时,调用该文件。v  Pages 使用po模式设计的测试页面n  BasePage.py  基类,对一些测试页面公共方法、属性的封装及webdrive一些方法的二次封装;n  Bing.py 测试页面。Report 测试报告v  Log 日志目录n  *****log日志。v  TestReport 测试报告目录n  ***html测试报告。TestCase 测试用例v  TC_bing.py。Run.py  控制测试用例的运行。

110,536

社区成员

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

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

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