题
我在后端运行了一个Windows应用程序。我将此应用程序中的函数映射到热键。就像我将一个消息框放入此函数并将热键提供为 Alt + Ctrl + D 。然后按 Alt , Ctrl 和 D 一起出现消息框。我的申请工作正常,直到这一点。
现在我想在这个函数中编写一个代码,这样当我使用记事本这样的另一个应用程序时,我选择一个特定的文本行并按下热键 Alt + Ctrl + D 它应该复制所选文本,并附加“_copied”;并将其粘贴回记事本。
任何尝试过类似申请的人都可以帮助我提供宝贵的意见。
解决方案
你的问题有两个答案
我的应用如何设置全局热键
你必须调用名为RegisterHotKey的API函数
BOOL RegisterHotKey(
HWND hWnd, // window to receive hot-key notification
int id, // identifier of hot key
UINT fsModifiers, // key-modifier flags
UINT vk // virtual-key code
);
更多信息: http://www.codeproject.com/KB/system/ nishhotkeys01.aspx
如何从前台窗口获取所选文本
最简单的方法是将crl-C发送到窗口,然后捕获剪贴板内容。
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
static public extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
.....
private void SendCtrlC(IntPtr hWnd)
{
uint KEYEVENTF_KEYUP = 2;
byte VK_CONTROL = 0x11;
SetForegroundWindow(hWnd);
keybd_event(VK_CONTROL,0,0,0);
keybd_event (0x43, 0, 0, 0 ); //Send the C key (43 is "C")
keybd_event (0x43, 0, KEYEVENTF_KEYUP, 0);
keybd_event (VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);// 'Left Control Up
}
免责声明:Marcus Peters的代码: http://bytes.com/forum/post1029553 -5.html 结果 发布在这里是为了您的方便。
不隶属于 StackOverflow