调用时调用某个SendKey,而不是一直发送

时间:2013-08-30 19:15:36

标签: vb.net symbols

这是我想要完成的想法。

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

Private Sub Timer1_Tick(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Timer1.Tick         On Error Resume Next

    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

2 个答案:

答案 0 :(得分:0)

为什么不使用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)

答案 1 :(得分:0)

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