By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,553 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,553 IT Pros & Developers. It's quick & easy.

Tracking key combination

P: n/a
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?
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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- ><>
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.