I've got a low-level keyboard hook which currently allows me to control a media player from inside any application, including games.
It works by looking at the specific keystroke captured.
I'd like to extend it to look for combinations of keys. I can hardcode some logic but I feel there must be a more logical way.
Really, I'm looking for something like a shift register...
eg as I type "BlahPlay", I'd like a structure which can be used like this...
[_,_,_,_]
[_,_,_,B]
[_,_,B,l]
[_,B,l,a]
[B,l,a,h]
[l,a,h,P]
[a,h,P,l]
[h,P,l,a]
[P,l,a,y] <-- detect "Play"
Short of looping and shifting the items in an array, what's the correct way to do this?
The best I can think of right now is ...
Private Register(4) As Char
Private Position = 0
Sub Add(Character as Char)
Register(Position) = Character
Position = (Position + 1) Mod Register.Length
End Sub
Function Get() as String
Dim Ret As New StringBuilder
For i = Position To Position + Register.Length
Ret.Append(Register(i Mod Register.Length)
Next
Return Ret.ToString
End Function
Instead of shifting the items in the buffer, you could create standard array and just inserting each item in the next position, looping around as necessary. Consider the following code:
Now you simply insert every new character at
position
and setposition = (position + 1) % buffSize
every time a new key is entered. Suppose you typedabcPlay
, the buffer would be:This means that instead of simply reading each char out from the begin reading at
position
. For example, if you're testing to see if the buffer containsPlay
, you'd have to do something like this:Edit You edited your question since I began writing this answer. It appears that you've already developed a solution pretty close to this. I'd still recommend going with something like this. It will be more efficient than shifting each item in the array on every insert.