【发布时间】:2016-05-18 06:07:31
【问题描述】:
我正在编写我的 SDL Trados Studio 插件。
插件的最后一部分需要一些 API 根本没有公开的自动化,所以我所拥有的(坚持一些事情)就是自动化默认的键盘快捷键。
我的代码非常适合英语键盘布局(还有匈牙利语!),但它当然不适用于希腊语、俄语等。
我一直在寻找解决方案,但直到现在我都无法找到它,不是在网络上也不是在 SO 上,例如这个帖子:Change keyboard layouts through code c#
我需要将键盘布局更改为英文,以便它可以使用正确的快捷键(和其他字符串)。然后我需要将其切换回以前的状态。我正在使用非常有限的 API,所以我只能使用 SendKeys。
这是工作代码:
//Save the document
SendKeys.SendWait("^s");
//Open files view
SendKeys.SendWait("%v");
SendKeys.SendWait("i");
SendKeys.SendWait("1");
Application.DoEvents();
//get url and credentials from a custom input form
string[] psw = UploadData.GetPassword(
Settings.GetValue("Upload", "Uri", ""),
Vars.wsUsername == null ? Settings.GetValue("Upload", "User", "") : Vars.wsUsername,
Vars.wsPassword == null ? "" : Vars.wsPassword
);
Application.DoEvents();
if (psw != null)
{
try
{
//start upload
SendKeys.SendWait("%h");
SendKeys.Send("r");
//select all files
SendKeys.Send("%a");
SendKeys.Send("%n");
//enter login url
SendKeys.Send("%l");
SendKeys.Send("{TAB}");
SendKeys.Send(psw[0]);
SendKeys.Send("{TAB}");
SendKeys.Send("{ENTER}");
//enter username
SendKeys.Send("%l");
SendKeys.Send("+{END}");
SendKeys.Send(psw[1]);
//enter credentials
SendKeys.Send("%p");
SendKeys.Send(SendEscape(psw[2]));
SendKeys.Send("{ENTER}");
//start upload
SendKeys.SendWait("%f");
}
catch (Exception)
{
MessageBox.Show("Cannot do automatic upload, please use the default method of Trados Studio.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
//switch back to editor view
SendKeys.SendWait("%vd");
}
}
所以我的问题是:
谁能帮我写一个代码来实际存储当前的键盘布局并切换到英文,然后在最后切换回来?
有没有更简单的解决方案?我试图查看本机方法,但它对我来说太高了,所以如果这是代替切换键盘布局的方法,我将非常感谢将我的代码转换为本机的任何帮助。有什么建议吗?
【问题讨论】:
标签: c# .net keyboard pinvoke sendkeys