Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Below is the section I'm having trouble with at the moment:
VB
Friend Sub glbHooks_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles glbHooks.KeyPress
    newStr = newStr & LCase(e.KeyChar)
    n = 0

    If e.KeyChar = vbBack Then
        tmpStr = ""
        For i = 1 To newStr.Length
            If i < newStr.Length - 1 Then tmpStr = tmpStr & newStr.Substring(i - 1, 1)
        Next
        newStr = tmpStr
    End If

    If newStr.Length = 4 Then
        If newStr(3) = "-" Then
            If InStr(rkSet, newStr) Then
                n = 1
                My.Computer.Keyboard.SendKeys(Chr(8) & Chr(8) & Chr(8) & dicRepStr(newStr))
                newStr = ""
            End If

        End If

        tmpStr = ""
        For i = 1 To newStr.Length
            If i <> 1 Then tmpStr = tmpStr & newStr.Substring(i - 1, 1)
        Next
        newStr = tmpStr
    End If

    If n = 1 Then
        Sleep(100)
        My.Computer.Keyboard.SendKeys(Chr(8) & " ")
    End If
End Sub


What this is supposed to do is replace a 4-char usercode with user's choice of text. It works great but it adds a dash {-} at the end of the replacement text. The user chooses which symbol is the trigger and in my tests I'm using the dash. Which means the program is including the last-typed character.

I added the last "If" statement to try and remove it then add a space but it does no good. It just puts the dash AFTER the space.

Also, if I put "If n = 1 Then My.Computer.Keyboard.SendKeys(Chr(8))" at the beginning of this Sub then it will remove the dash after typing the next character. BTW the "Chr(8)" I tried when the "vbBack" wasn't taking out the dash, that's the only reason it's there now. Also, I originally had 4 "vbBack" {Chr(8)} in the replacing portion but have changed it around to try to get rid of that final character.

How do I get it so it won't send the dash {or whatever is chosen as the trigger symbol}?

Any help in this would be appreciated!

Peace to you and yours,
Matthew "Dra'Gon" Stohler
Posted
Updated 23-Mar-12 15:50pm
v2
Comments
BlueDragonFire 24-Mar-12 20:13pm    
Okay, if I put a msgbox before it displays the "dicRepStr(newStr)" then it doesn't put in the trigger character.
--> MsgBox("")
So is there a way to spoof a message box or something similar? I don't want to have to click "Okay" on the message box every time I use the hotkeys. I've tried sleep {for 100 & 500} but it didn't work.

Peace to you and yours,
Matthew "Dra'Gon" Stohler

I could not understand exactly how replacement is done etc.
But, if a key is pressed by the user and the key is not required to be entered in to the control, then after handling in the KeyPress event you can set e.Handled = true
as explained here.
http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventhandler.aspx[^]
I think it may be helpful to you.
 
Share this answer
 
Comments
BlueDragonFire 24-Mar-12 19:01pm    
Thanks for trying, but it doesn't get rid of it. I tried it in the Keypress event as well as the Keyup event with no change.

Though some of the code on that page may come in handy for this project. So thanks for that. :D

Peace to you and yours,
Matthew "Dra'Gon" Stohler

-- Didn't see that "Have a Question or Comment" link before ;) .
I'm not sure how this code is to be used, so I set up a test program with a single textbox and with newStr defined as a form-level string. I tried this code in both the textbox.KeyPress and Form.KeyPress handlers. In both cases it did the replacement but left the first character of the 4-char code within the textbox, which I fixed by adding a fourth chr(8) to the replacement string.

I could only get the routine to put in the extra "-" character by changing the code to use
VB
Textbox.Text &= chr(8) ... 'etc
instead of SendKeys and, when I did this, it actually put the caret and the "-" character at the start of the text box!

I found that setting e.KeyChar=Chr(0) prevented the "-" character from appearing in the textbox.

I also simplified the string handling by removing the "n" & "tmpStr" variables and the for-loops and this is the code I ended up with;

VB
Static newStr As String

newStr &= LCase(e.KeyChar)

If e.KeyChar = vbBack Then
   newStr = If(newStr.Length > 1, newstr.substring(0, newstr.length-1), newStr)
End If

If newStr.Length = 4 Then
    If newStr(3) = "-" AndAlso InStr("123-", newStr) Then
        My.Computer.Keyboard.SendKeys(Chr(8) & Chr(8) & Chr(8) & Chr(8) & dicRepStr(newStr))
        newStr = ""
    Else
        newStr = newStr.Substring(1)
    End If
End If


Hope this helps.
 
Share this answer
 
Comments
BlueDragonFire 26-Mar-12 14:33pm    
I'm getting an "Expression expected" error on that "newStr = If..." line.

Tried the "e.KeyChar=Chr(0)" in a couple places but it still puts the trigger character at the end.

Basically what I'm doing with this is making an Auto Replacement program. Set it up with a list of 3-char hotkeys and a trigger symbol {"-" in this case}. Type in the hotkeys and the trigger then it replaces those 4 characters with whichever string is assigned to that.
Example:
qbf- = The quick brown fox jumped

Type in qbf- in any text window {browser, Notepad, etc...} and it will come out with your text. I'm using an INI file to save the assignments then using the Dictionary method {"dicRepStr(newStr)"} when the program loads.

If I use 4 "Chr(8)" or "vbBack" then it removes whatever character was prior to entering the hotkeys. If I use 3 with the replacement string and one after then it removes a character from the string I want to keep. Both ways add the trigger char at the end.

Your coding certainly looks less messy than mine does :D . Just that Error to deal with {unfortunately I don't know enough to understand how to fix that line on my own} then I can check to see if it works for this.

Thanks for your help in this!!

Peace to you and yours,
Matthew "Dra'Gon" Stohler
BlueDragonFire 26-Mar-12 16:08pm    
EDIT: I forgot to mention that I'm using "SendKeys" because it's being used outside of the program, in other windows, so the "***.Text =" won't work.
Now I understand the cause of your problem - I hadn't realized that the text was being entered into a different program. The previous suggestions of setting e.Handled=True or setting e.KeyChar=chr(0) assumed that the text was entered into the same program as was doing the substitution. In order to do what you want you would need to clear the pending character from the input buffer of the active program and I don't know how to do that I'm afraid.

The nearest I can get is to delete the extra character after it has been appended to the text. Because the substitution occurs before the unwanted character is appended, I've deferred the deletion until the next character is entered (see variable "SubstitutionMade") so you can just keep typing the rest of your text and the extra character will get removed automatically.

I used example code from http://sim0n.wordpress.com/2009/03/28/vbnet-keyboard-hook-class/[^] to provide a global keyboard hook.

VB
Private WithEvents kbHook As New KeyboardHook

 Private Sub kbHook_KeyDown(ByVal Key As System.Windows.Forms.Keys) Handles kbHook.KeyDown
     Static newStr As String
     Static SubstitutionMade As Boolean

     If SubstitutionMade Then
         My.Computer.Keyboard.SendKeys(Chr(8))
         SubstitutionMade = False
     End If

     newStr &= LCase(Chr(Key))

     If Key = Keys.Back AndAlso newStr.Length > 1 Then
         newStr = newStr.Substring(0, newStr.Length - 1)
     End If

     If newStr.Length = 4 Then
         If Key = Keys.OemMinus AndAlso InStr("xyz", newStr.Substring(0, 3)) >= 1 Then   'Instr(rkset, newStr.substring(0,3))
             SubstitutionMade = True
             My.Computer.Keyboard.SendKeys(Chr(8) & Chr(8) & Chr(8) & "ABCD")   'dicRepStr(newStr))
             newStr = ""
         Else
             newStr = newStr.Substring(1)
         End If
     End If
 End Sub


For testing I used a hard-coded "xyz-" as the 4-character code and a hard-coded "ABCD" as its replacement; these values are still in the code but the trailing comments show what you will need to change.

Note: the keyboard hook supplies a Windows.Forms.Keys value which wouldn't match with "-", which is why I used
VB
Key = Keys.OemMinus
and is why I changed the dictionary look-up to only match the 1st three characters (if you always terminate on a "-" then there is no need to store that character anyway).

Hope this helps
 
Share this answer
 
v2
Comments
BlueDragonFire 27-Mar-12 15:52pm    
Yeah, I was able to code it so it would be removed at the next keystroke, and that's probably the way I'll have to go it looks like.

I'm using a Global Keyhook in this already which I found online. It's doing great for getting the typed keys. I might take a look "under it's hood" and see if I can figure out how to get rid of that character. I'll probably just get lost though, I'm still very much an amateur at coding.

Thanks for your help!!!

Peace to you and yours,
Matthew "Dra'Gon" Stohler

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900