[英]How to send keystrokes to application UI automation -C#
我需要找出關鍵板值的 automationid 嗎? 如何將擊鍵發送到應用程序 UI 自動化? 我需要在鍵盤中自動執行向上翻頁和向下翻頁功能。 最好的方法是什么?
編輯:在我的應用程序中遵循該過程。 假設最終用戶打開了 5 頁的 MS Word 文檔,他按下向上翻頁和向下翻頁按鈕在這些頁面內移動。 我想使用 c# 自動化這個場景。 目前我使用了 UIAutomationClient.dll 和 UIAutomationTypes.dll。 我可以用這些嗎?
自動發送各種擊鍵的一個很好的方法是AutoIt 自動化和腳本語言。 它可以做很多事情,尤其是向上翻頁和向下翻頁發送。
我建議用 autoit 編寫一個 EXE,它將自己連接到它將向其發送擊鍵的程序。
SendWait("C")
。例如
// 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("=");
}
請參閱如何:在代碼中模擬鼠標和鍵盤事件>“將擊鍵發送到不同的應用程序”
我認為您想將擊鍵發送到您沒有源代碼的應用程序。
我不能幫你告訴你如何直接在 C# 中完成。
但是你可以用AutoIt很容易地做到這一點; 它有一個 DLL,您可以在 C# 中引用它來完全滿足您的需要。
您是否閱讀過如何將鍵而不是字符發送到進程?
這會准確地告訴您如何將密鑰發送到應用程序。
// 使用 Windows 窗體並將此列表與發送鍵一起使用https://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm我很確定他只是在詢問他的 SendKey.Send 的鍵值("{}");
在這種情況下,將 word 文檔置於前台,然后在屏幕鍵盤上使用System.Diagnostics.Process.Start("osk.exe");
並使用鼠標輸入單擊向上和向下翻頁按鈕,因為鼠標單擊需要向上和向下翻頁按鈕的屏幕坐標。
(我嘗試使用 UI 自動化檢測屏幕鍵盤。但它沒有檢測到屏幕的按鍵。https://stackoverflow.com/questions/11077738/windows-sdk-inspect-tool-what-are-the-reasons -on-screen-keyboard-does-not-disp無法找到解決此問題的方法。因此,我使用此移動點擊方法來點擊按鈕。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.