对于SendKeys,%-符号是Alt键的代码。
但是我没有找到一种发送纯%符号的方法。
下面的console-application-program展示了它。您必须在此之前启动notepad.exe。
然后,您在记事本中只看到“percent sign=”,没有%。按键后,程序将"%B“发送到记事本
它在德国系统上打开编辑-菜单(Alt-B --> "Bearbeiten“= "Edit")。
msdn-help解释了+^和%符号的功能,但没有解释我必须做什么才能发送这个符号。有什么建议吗?
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace SendTextToNotepadSendKeys
{
class Program
{
[DllImport("user32.dll")]
public static extern int FindWindow(
string lpClassName, // class name
string lpWindowName // window name
);
[DllImport("user32.dll")]
public static extern int SetForegroundWindow(
int hWnd // handle to window
);
static void Main(string[] args)
{
Console.WriteLine("At least one instance of notepad must be running! if ready press a key!");
Console.ReadKey();
Console.WriteLine("");
int handleThisWindow = (int)Process.GetCurrentProcess().MainWindowHandle;
int handleNotepad = FindWindow(null, "Unbenannt - Editor"); // change to your windows title - this is the german one
if (handleNotepad == 0)
{
Console.WriteLine("notepad window not found - possibly you have to change the window title in program code! Please press a key!");
Console.ReadKey();
}
else
{
// >>>>>>>>>>> here you see it <<<<<<<<<<<<<<
SetForegroundWindow(handleNotepad);
System.Windows.Forms.SendKeys.SendWait("percent-sign=%"); // the text ends with "="
SetForegroundWindow(handleThisWindow);
Console.WriteLine("the text in notepad ends with '='");
Console.WriteLine("% is representing the Alt-key, we show it with the following code");
Console.WriteLine("on german machines Alt-B will open the Edit-menu - possibly change 'B' in the code for your machine");
Console.ReadKey();
SetForegroundWindow(handleNotepad);
System.Windows.Forms.SendKeys.SendWait("%B");
Console.ReadKey();
}
}
}
}发布于 2018-01-22 04:19:21
要发送文字%,请用大括号将其括起来:{%}。
对SendKeys来说,加号(+)、插入符号(^)、百分号(%)、代字号(~)和括号()具有特殊的含义。要指定这些字符中的一个,请将其括在大括号({})中。例如,要指定加号,请使用"{+}“。要指定大括号字符,请使用"{{}“和"{}}”。方括号()对SendKeys没有特殊意义,但必须将它们括在大括号中。在其他应用程序中,括号具有特殊的含义,在发生动态数据交换(DDE)时可能非常重要。
发布于 2020-07-09 07:30:55
我在VBS上使用了这个函数,它很容易理解它在做什么,只需移植到您首选的语言即可。使用您的文本字符串进行调用,例如,对于VBS:
strText = "My%Text~With+Escape^Chars"
strText = EscapeChars(strText)
您将获得一个用括号括起的%, ~, +,和^返回字符串,该字符串在SendKeys中非常有效,即"My{%}Text{~}With{+}Escape{^}Chars"
Function EscapeChars(vStr)
On Error Resume Next
For i = 1 to Len(vStr)
If Grep("[%()~!+^]", Mid(vStr, i, 1)) Then
EscapeChars = EscapeChars & "{" & Mid(vStr, i, 1) & "}"
Else
EscapeChars = EscapeChars & Mid(vStr, i, 1)
End If
Next
End Function发布于 2021-06-04 03:11:39
感谢Rob,这是它成功的唯一原因,我测试过:%)(巫师是最有可能发生的。
我不得不修改你的代码,GREP部分不适合我,所以我这样做了:
Function EscapeChars(vStr)
On Error Resume Next
For i = 1 to Len(vStr)
If "%" = Mid(vStr, i, 1)_
Or "+" = Mid(vStr, i, 1)_
Or "^" = Mid(vStr, i, 1)_
Or "!" = Mid(vStr, i, 1)_
Or "(" = Mid(vStr, i, 1)_
Or ")" = Mid(vStr, i, 1)_
Or "[" = Mid(vStr, i, 1)_
Or "]" = Mid(vStr, i, 1)_
Or "~" = Mid(vStr, i, 1) Then
EscapeChars = EscapeChars & "{" & Mid(vStr, i, 1) & "}"
Else
EscapeChars = EscapeChars & Mid(vStr, i, 1)
End If
Next
End Function我基本上只需要放入大量的Or语句,它就像一个护身符一样工作,谢谢。
https://stackoverflow.com/questions/48371261
复制相似问题