【问题标题】:Call for certain SendKey when called, not send all the time调用时调用某个 SendKey,而不是一直发送
【发布时间】:2013-09-03 14:28:55
【问题描述】:

这就是我想要实现的想法。

在文本框中,我输入“增加 50%”。我点击了另一个按钮来显示确切的文本。数字、字母和一些符号正确显示。但是,某些符号,例如 % ^ ( 和 ) 不会显示。我发现我必须使用 SendKeys.Send("{%}") 函数来发送该特定键和其他一些键。好吧,没什么大不了的。但是我想要完成的是,当我使用 SendKeys.Send("{%}") 函数时,它会在 OUTPUT 上发送 % EVERYTIME,即使我可能输入了一些不同的东西,但没有包含它.基本上我想要 % WHEN I CALL IT,而不是一直。我希望这有帮助。这也是我的代码。

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 Timer1.Tick 出错时继续下一步

    If CheckBox1.Checked = True Then 'Only Checked Items of the CheckedListbox

        If IntCount > CheckedListBox1.CheckedItems.Count - 1 Then 'If the index is higher then the max. index of the checkedlistbox
            IntCount = 0 ' The index will reset to 0
        End If
        SendKeys.SendWait(CheckedListBox1.CheckedItems(IntCount).ToString & "{ENTER}") 'Send keys to the active windows
        IntCount += 1 'Goto the next line

    Else 'All items

        If IntCount > CheckedListBox1.Items.Count - 1 Then 'If the index is higher then the max. index of the checkedlistbox
            IntCount = 0 ' The index will reset to 0
        End If
        SendKeys.SendWait(CheckedListBox1.Items(IntCount).ToString & "{ENTER}") 'Send keys to the active windows
        IntCount += 1 'Goto the next line
        SendKeys.Send("{%}") 'HERE THE % SYMBOL IS DISPLAYED EVERYTIME. NOT WHAT I WANT! ONLY WHEN I CALL IT IN THE INPUT TEXTBOX!


    End If

【问题讨论】:

  • 你真的需要改写你的问题,我不知道你到底在问什么
  • 尝试实现一个代码,其中“如果我的文本包含 %,请将 % 替换为“{%}”。不确定将其放在哪里..

标签: vb.net symbols


【解决方案1】:

为什么不直接使用String.Replace 将% 符号替换为{%},因为当您在SendKeys.Send 中使用它时,如果您要发送的文本不包含它,那么它将只返回字符串而不进行修改。

Dim sendThis as string = "blabla is at 100%"
SendKeys.Send(sendThis.Replace("%","{%}"))

如果由于某种原因您不能或不想替换它,您可以检查您的文本是否包含 % 并根据该条件发送或不发送它

If CheckedListBox1.Items(IntCount).ToString.Contains("%") Then
    SendKeys.Send("{%}")
End If

这就是你的代码使用方法 1 的样子

If CheckBox1.Checked = True Then
    If IntCount > CheckedListBox1.CheckedItems.Count - 1 Then
        IntCount = 0
    End If
    SendKeys.SendWait(CheckedListBox1.CheckedItems(IntCount).ToString & "{ENTER}")
    IntCount += 1
Else
    If IntCount > CheckedListBox1.Items.Count - 1 Then
        IntCount = 0
    End If
    'Replace % with {%}
    SendKeys.SendWait(CheckedListBox1.Items(IntCount).ToString.Replace("%", "{%}").ToString.Replace("^", "{^}").ToString.Replace("+", "{+}") & "{ENTER}")
    IntCount += 1
End If

替代方法(更好的性能)

Dim sendText as string = CheckedListBox1.Items(IntCount).ToString
sendText = SendText.Replace("%", "{%}")
sendText = SendText.Replace("^", "{^}")
sendText = SendText.Replace("+", "{+}")
SendKeys.SendWait(sendText)

【讨论】:

  • 嘿拉斐尔。谢谢大佬的回复。我看过你的代码,我很确定这就是我要找的。我遇到的问题是放在哪里。
  • @Serendipitepic 如果您使用第一种方法,那么您只需要 1 个 SendKeys.Send 调用(并在末尾添加一个 {ENTER} )并删除您在其中的 2,对于第二种方法只需将 SendKeys.Send("{%}") 替换为这 3 行,您必须对其进行一些重组以最后发送 `{ENTER} 键,总而言之,我认为第一种方法是最好的。
  • @Serendipitepic 检查更新的答案我已经包含了在您的代码中使用方法 1 的样子。
  • 天啊老兄,你是 MF'n MANN!非常感谢!
  • 抱歉 Raphael 再次打扰您,但是替换多个变量怎么样。喜欢替换 % 和 ^ 和 +?
【解决方案2】:

SendKeys 对某些字符有特殊含义,包括加号+、插入符号^、百分号%、波浪号~、圆括号() 和大括号{}。发送这些字符时,您必须将它们括在大括号 {} 中。此外,出于兼容性原因,您必须将括号 [] 括起来。
如果您发送的文本被编码到您的程序中,您可以手动将这些字符换行。
但如果文本是用户指定的,或者来自其他来源,您可以像这样包装这些字符:

Dim SendText as String = "This ^string^ might+need (to be) wrapped."
Dim sb as new Text.StringBuilder(SendText)
Dim i as Integer = 0
While i < sb.Length
  If "+^%~(){}[]".Contains(sb.Chars(i)) Then
    sb.Insert(i, "{"c)
    sb.Insert(i + 2, "}"c)
    i += 3
  Else
    i += 1
  End If
End While
SendKeys.Send(sb.ToString())

您还可以发送其他击键,包括退格键和箭头键:

SendKeys.Send("{BACKSPACE}")
SendKeys.Send("{DOWN}")

欲了解更多信息,SendKeys on MSDN

【讨论】:

  • 这不会按原样工作,并且会给你一个类似于Keyword "{{" is not valid. 的运行时错误,因为你每次迭代都会更改SendText 字符串,导致它替换你刚刚的左括号和右括号添加,如果你将它分配给一个新的字符串,那么它会起作用。否则是处理此类问题的好方法
  • @RaphaelSmit 我修复了它,第二次替换了它自己的括号。
猜你喜欢
  • 2016-05-27
  • 2011-07-27
  • 1970-01-01
  • 2016-09-25
  • 2020-04-03
  • 2018-02-10
  • 2011-08-21
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多