首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用System.Windows.Forms.SendKeys.SendWait发送%-符号

如何使用System.Windows.Forms.SendKeys.SendWait发送%-符号
EN

Stack Overflow用户
提问于 2018-01-22 04:13:07
回答 3查看 767关注 0票数 1

对于SendKeys,%-符号是Alt键的代码。

但是我没有找到一种发送纯%符号的方法。

下面的console-application-program展示了它。您必须在此之前启动notepad.exe。

然后,您在记事本中只看到“percent sign=”,没有%。按键后,程序将"%B“发送到记事本

它在德国系统上打开编辑-菜单(Alt-B --> "Bearbeiten“= "Edit")。

msdn-help解释了+^和%符号的功能,但没有解释我必须做什么才能发送这个符号。有什么建议吗?

代码语言:javascript
复制
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();
            }
        }
    }
}
EN

回答 3

Stack Overflow用户

发布于 2018-01-22 04:19:21

要发送文字%,请用大括号将其括起来:{%}

More information from MSDN

对SendKeys来说,加号(+)、插入符号(^)、百分号(%)、代字号(~)和括号()具有特殊的含义。要指定这些字符中的一个,请将其括在大括号({})中。例如,要指定加号,请使用"{+}“。要指定大括号字符,请使用"{{}“和"{}}”。方括号()对SendKeys没有特殊意义,但必须将它们括在大括号中。在其他应用程序中,括号具有特殊的含义,在发生动态数据交换(DDE)时可能非常重要。

票数 2
EN

Stack Overflow用户

发布于 2020-07-09 07:30:55

我在VBS上使用了这个函数,它很容易理解它在做什么,只需移植到您首选的语言即可。使用您的文本字符串进行调用,例如,对于VBS:

strText = "My%Text~With+Escape^Chars"

strText = EscapeChars(strText)

您将获得一个用括号括起的%, ~, +,^返回字符串,该字符串在SendKeys中非常有效,即"My{%}Text{~}With{+}Escape{^}Chars"

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2021-06-04 03:11:39

感谢Rob,这是它成功的唯一原因,我测试过:%)(巫师是最有可能发生的。

我不得不修改你的代码,GREP部分不适合我,所以我这样做了:

代码语言:javascript
复制
    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语句,它就像一个护身符一样工作,谢谢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48371261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档