博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# using Sendkey function to send a key to another application
阅读量:2239 次
发布时间:2019-05-09

本文共 808 字,大约阅读时间需要 2 分钟。

If notepad is already started, you should write:

// import the function in your class[DllImport ("User32.dll")]static extern int SetForegroundWindow(IntPtr point);//...Process p = Process.GetProcessesByName("notepad").FirstOrDefault();if( p != null){    IntPtr h = p.MainWindowHandle;    SetForegroundWindow(h);    SendKeys.SendWait("k");}

returns an array of processes, so you should get the first one (or find the one you want).

If you want to start notepad and send the key, you should write:

Process p = Process.Start("notepad.exe");p.WaitForInputIdle();IntPtr h = p.MainWindowHandle;SetForegroundWindow(h);SendKeys.SendWait("k");

The only situation in which the code may not work is when notepad is started as Administrator and your application is not.

转载于:https://www.cnblogs.com/zeroone/p/3634590.html

你可能感兴趣的文章
手抄笔记:深入理解linux内核-1
查看>>
内存堆与栈
查看>>
Leetcode C++《每日一题》20200621 124.二叉树的最大路径和
查看>>
Leetcode C++《每日一题》20200622 面试题 16.18. 模式匹配
查看>>
Leetcode C++《每日一题》20200625 139. 单词拆分
查看>>
Leetcode C++《每日一题》20200626 338. 比特位计数
查看>>
Leetcode C++ 《拓扑排序-1》20200626 207.课程表
查看>>
Go语言学习Part1:包、变量和函数
查看>>
Go语言学习Part2:流程控制语句:for、if、else、switch 和 defer
查看>>
Go语言学习Part3:struct、slice和映射
查看>>
Go语言学习Part4-1:方法和接口
查看>>
Leetcode Go 《精选TOP面试题》20200628 69.x的平方根
查看>>
leetcode 130. Surrounded Regions
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
【JMeter】1.9上考试jmeter测试调试
查看>>
【虫师】【selenium】参数化
查看>>
【Python练习】文件引用用户名密码登录系统
查看>>
学习网站汇总
查看>>