kr******@detandetfirma.dk (Kristian) wrote in message news:<c9**************************@posting.google. com>...
How do I track a key combination like this '<abcd'?
I have trapped the '<' key using keypressed in a jtextpanes
keylistener. But what to do now? how do I trap the next lets say 3
charactes typed by the user?
Create a char array, and store the characters in it using a counter which
indicates the next free index. To avoid having to resize or copy the
array, make the counter wrap around to the start of the array when it
exceeds the upper bounds: cnt=cnt%array.length; Then check the previous
n chars (remembering to wrap to the end if exceeding the lower bounds of
the array) for the required character sequence, where 'n' is the length
of the search string.
Suppose we are looking for the string ABCDE, and the user types xyABCDE:
(our array is the same size as the search string, which makes life easier)
----- Nothing typed.
^
xy--- User types xy, counter at [2].
^
xyABC User adds ABC, counter has wrapped to [0].
^
DEABC User adds DE, counter at [2]. Our required sequence is at
^ [2]-->[1] ( [2]-->[4], then [0]-->[1] ) which we can check
by looping [2]-->[6] while %'ing so 5 and 6 wrap to 0 and 1.
Modifying this scheme to check for different strings of varying sizes
requires a little bit more work, but not much.
-FISH- ><>