Board index » delphi » Translating key messages.

Translating key messages.

Okay folks. Here's a nice problem for you. I've got a whole load of
keyboard messages, obtained by using SetWindowsHookEx, in a nice long
list. I basically want to convert the stuff into roughly ASCII text. The
criteria are:

Output must have all alphanumeric characters and punctuation symbols
correct, with the right case. Bering able to get the ` and ? would be
nice as well... so the state of the shift key must be taken into
account.

Delete and return and backspace can just be put into the stream of ascii
chars as <del> <ret> <backsp>. Modifier keys such as control and alt, it
would be nice to have <control down> (some stuff) <control up>.

Numeric keypad stuff would be useful if it's being used to type numbers,
and the various arithmetic signs around the keypad must also be
reflected correctly.

I don't give a damn about function keys, Print Screen, Scoll Lock, Pause
/ Break, Insert, Home / End, Page UP / Page Down or Escape.

Basically I'd like an app to read a binary (or even ascii) file which
consists of a whole load of recorded keyboard messages, and generate
(slightly odd looking) ASCII text.

Any ideas??

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
"ALGOL 60 was a language so far ahead of its time that it
was not only an improvement on its predecessors but also
on nearly all its successors". C.A.R. Hoare
--------------BEGIN GEEK CODE BLOCK--------------
Version: 3.12
GCS/CC d(+) s-:- a-- C+++$ UL@ P L@>++ E- W++
N+++ o-- K++ w+++$ O--- M-- V-- PS@ Y-- PGP-
t--- 5-- X-- R-- !tv b+ DI+ D+ G e++ h- r z++>---
---------------END GEEK CODE BLOCK---------------

 

Re:Translating key messages.


On Thu, 04 Feb 1999 21:41:32 +0000, Martin Harvey

Quote
<mar...@aziraphale.demon.co.uk> wrote:
>Okay folks. Here's a nice problem for you. I've got a whole load of
>keyboard messages, obtained by using SetWindowsHookEx, in a nice long
>list. I basically want to convert the stuff into roughly ASCII text. The
>criteria are:

>Output must have all alphanumeric characters and punctuation symbols
>correct, with the right case. Bering able to get the ` and ? would be
>nice as well... so the state of the shift key must be taken into
>account.

>Delete and return and backspace can just be put into the stream of ascii
>chars as <del> <ret> <backsp>. Modifier keys such as control and alt, it
>would be nice to have <control down> (some stuff) <control up>.

>Numeric keypad stuff would be useful if it's being used to type numbers,
>and the various arithmetic signs around the keypad must also be
>reflected correctly.

>I don't give a damn about function keys, Print Screen, Scoll Lock, Pause
>/ Break, Insert, Home / End, Page UP / Page Down or Escape.

>Basically I'd like an app to read a binary (or even ascii) file which
>consists of a whole load of recorded keyboard messages, and generate
>(slightly odd looking) ASCII text.

>Any ideas??

The API function ToAscii might help.  You need a keyboard state buffer
to go with the keyboard message, which could be a little hard to come
by.  It's easier to do the translation on a live message stream, where
you can use GetKeyboardState.  A keyboard state buffer is 256 bytes
long, but for ToAscii only has 257 relevant bits, so you might squeeze
it down and store it, or you could try to recreate it from the keyboard
message stream.

ToAscii works best if you're trying to get output that looks pretty much
like ASCII text.  But if you're doing that, why not just trap WM_CHAR
messages?

--
Report all {*word*201} mail to your Potsmaster.

Re:Translating key messages.


Quote
David Rifkind wrote:

> ToAscii works best if you're trying to get output that looks pretty much
> like ASCII text.  But if you're doing that, why not just trap WM_CHAR
> messages?

Oh yeah... I can use WH_GETMESSAGE But is this going to let me trap
system wide WM_CHAR messages? Well... I'll give it a go :-)

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Re:Translating key messages.


Quote
David Rifkind wrote:

> ToAscii works best if you're trying to get output that looks pretty much
> like ASCII text.  But if you're doing that, why not just trap WM_CHAR
> messages?

Trapping ASCII char messages works just great. A little playing around
has to be done with return and delete, but apart from that, it's as easy
as ABC, provided you don't want to do anything fancy with control and
alt etc etc.

I can send you a simple test program that I've done if you'd like it!

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Re:Translating key messages.


I seem to have encountered a couple of additional problems. A lot of
applications seem to post multiple copies of WM_CHAR messages onto the
queue :-( So far, I've found that most simple apps (Telnet, Notepad etc)
don't do this, and more fancy apps, such as word and netscape do!

Just to show you how annoying this is, this is the result of monitoring
the WM_CHAR messages that get posted into the message queue by netscape
in the course of writing this email.

-----------------------------------------------------------------

AAddddiittiioonnaall  pprroobblleemmss..  ((WWaass::  ))..<BS><BS>

II  sseeeemm  ttoo  hhaavvee  eennccoouunntteerreedd  aa  ccoouuppllee
ooff  aaddddiittiioonnaall  pprroobblleemmss..  AA  lloott  ooff
aapppplliiccaattiioonnss,,<BS><BS>  sseeeemm  ttoo  ppoosstt
mmuullttiippllee  ccooppiieess  ooff  WWMM__CCHHAARR  mmeessssaaggeess
oonnttoo  tthhee  qquueeuuee  ::--((  SSoo  ffaarr,,  II''vvee  
-----------------------------------------------------------------

Any smart ideas on how to get around this??

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Re:Translating key messages.


On Sat, 06 Feb 1999 21:54:18 +0000, Martin Harvey

Quote
<mar...@aziraphale.demon.co.uk> wrote:
>I seem to have encountered a couple of additional problems. A lot of
>applications seem to post multiple copies of WM_CHAR messages onto the
>queue :-( So far, I've found that most simple apps (Telnet, Notepad etc)
>don't do this, and more fancy apps, such as word and netscape do!

>Just to show you how annoying this is, this is the result of monitoring
>the WM_CHAR messages that get posted into the message queue by netscape
>in the course of writing this email.

>-----------------------------------------------------------------

>AAddddiittiioonnaall  pprroobblleemmss..  ((WWaass::  ))..<BS><BS>

>II  sseeeemm  ttoo  hhaavvee  eennccoouunntteerreedd  aa  ccoouuppllee
>ooff  aaddddiittiioonnaall  pprroobblleemmss..  AA  lloott  ooff
>aapppplliiccaattiioonnss,,<BS><BS>  sseeeemm  ttoo  ppoosstt
>mmuullttiippllee  ccooppiieess  ooff  WWMM__CCHHAARR  mmeessssaaggeess
>oonnttoo  tthhee  qquueeuuee  ::--((  SSoo  ffaarr,,  II''vvee  
>-----------------------------------------------------------------

>Any smart ideas on how to get around this??

Have you checked your duplex switch?  Oh, wait, wrong generation.

--
Report all {*word*201} mail to your Potsmaster.

Re:Translating key messages.


'Cause you're getting two of every character, it looks like you're getting
the WM_CHAR messages which have the 31st bit of the lParam value is set.
This bit is 1 if the key is released, and 0 if the key is pressed.

Look up WM_CHAR in the Win32 SDK.

Also, I've found when I was writing a password-reading-in-the-background
program, that when I type, I tend to leave a lot of the keys down before I
release them.  So, if I didn't have the check for the key press or release
in, I'd get a stream of text like:

AdAdddititoonanall p prroboblleemsms..

Since it is the TranslateMessage function which posts the WM_CHAR messages,
I'm not sure how the particular application would handle this. One reason
could be that when they handle the WM_KEYDOWN message they don't translate
it if it is a key release.

Good luck,

Cameron
_______________________
cameronm...@hotmail.com

Quote
Martin Harvey wrote in message <36BCBA0A.AF818...@aziraphale.demon.co.uk>...
>I seem to have encountered a couple of additional problems. A lot of
>applications seem to post multiple copies of WM_CHAR messages onto the
>queue :-( So far, I've found that most simple apps (Telnet, Notepad etc)
>don't do this, and more fancy apps, such as word and netscape do!

>Just to show you how annoying this is, this is the result of monitoring
>the WM_CHAR messages that get posted into the message queue by netscape
>in the course of writing this email.

>-----------------------------------------------------------------

>AAddddiittiioonnaall  pprroobblleemmss..  ((WWaass::  ))..<BS><BS>

>II  sseeeemm  ttoo  hhaavvee  eennccoouunntteerreedd  aa  ccoouuppllee
>ooff  aaddddiittiioonnaall  pprroobblleemmss..  AA  lloott  ooff
>aapppplliiccaattiioonnss,,<BS><BS>  sseeeemm  ttoo  ppoosstt
>mmuullttiippllee  ccooppiieess  ooff  WWMM__CCHHAARR  mmeessssaaggeess
>oonnttoo  tthhee  qquueeuuee  ::--((  SSoo  ffaarr,,  II''vvee
>-----------------------------------------------------------------

>Any smart ideas on how to get around this??

>MH.

>--
>Martin Harvey.
>http://www.harvey27.demon.co.uk/mch24/
>PGP key available from above address,
>or http://wwwkeys.pgp.net/

Re:Translating key messages.


Quote
Cameron McCormack wrote:

> 'Cause you're getting two of every character, it looks like you're getting
> the WM_CHAR messages which have the 31st bit of the lParam value is set.
> This bit is 1 if the key is released, and 0 if the key is pressed.

Oddly enough, no! Having checked my bits fairly carefully, this isn't
the cause.. Take a look at WM_DEADCHAR!

Quote
> Look up WM_CHAR in the Win32 SDK.

Did that a long time ago....

Quote

> Since it is the TranslateMessage function which posts the WM_CHAR messages,
> I'm not sure how the particular application would handle this. One reason
> could be that when they handle the WM_KEYDOWN message they don't translate
> it if it is a key release.

This is what I suspect... the app is possibly posting or translating the
char twice... Just out of interest, I can't really ignore WM_CHAR
messages when the key is being released, just in case they key is being
held down. My current code looks like this:

type
    TCharRec=packed record
                  CharCode:WPARAM;
                  KeyData:LPARAM;
            end;

procedure WriteCharRec(var CharRec:TCharRec;Stream:TBlockMemoryStream);

var
   Character:char;
   RepCount,iterate:word;

begin
{Ignore characters with bit 31 or 30 set in the lparam}
if {((CharRec.KeyData and $C0000000)=0)}true then
begin
     Character:=Char((CharRec.CharCode and $00FF)); //Bottom 8 bits only
     {Now need to see how many times the thing has been repeated
      as a result of it being held down}
     RepCount:=CharRec.KeyData and $0000FFFF;
     for iterate:=0 to RepCount-1 do
     begin
          case Character of
               {CR}#13:begin
                            Character:=#13;

Stream.WriteBuffer(Character,SizeOf(Character));
                            Character:=#10;

Stream.WriteBuffer(Character,SizeOf(Character));
                       end;
               {DEL}#127:WriteString('<DEL>',Stream);
               #0..#12,#14..#31:WriteASCIICode(Character,Stream);
          else
              Stream.WriteBuffer(Character,SizeOf(Character));
          end;
     end;
end;
end;

procedure WriteASCIICode(Character:char;Stream:TBlockMemoryStream);

type
    StringArray=array[#0..#31] of string;
const
    ASCIICodes:StringArray =
    ('<NUL>','<SOH>','<STX>','<ETX>','<EOT>','<ENQ>','<ACK>','<BEL>',
     '<BS>','<HT>','<LF>','<VT>','<FF>','<CR>','<SO>','<SI>',
     '<DLE>','<DC1>','<DC2>','<DC3>','<DC4>','<NAK>','<SYN>','<ETB>',
     '<CAN>','<EM>','<SUB>','<ESC>','<FS>','<GS>','<RS>','<US>');

begin
WriteString(ASCIICodes[Character],Stream);
end;

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Re:Translating key messages.


On Sat, 06 Feb 1999 21:54:18 +0000, Martin Harvey

Quote
<mar...@aziraphale.demon.co.uk> wrote:
>I seem to have encountered a couple of additional problems. A lot of
>applications seem to post multiple copies of WM_CHAR messages onto the
>queue :-( So far, I've found that most simple apps (Telnet, Notepad etc)
>don't do this, and more fancy apps, such as word and netscape do!

>Just to show you how annoying this is, this is the result of monitoring
>the WM_CHAR messages that get posted into the message queue by netscape
>in the course of writing this email.

>-----------------------------------------------------------------

>AAddddiittiioonnaall  pprroobblleemmss..  ((WWaass::  ))..<BS><BS>

>II  sseeeemm  ttoo  hhaavvee  eennccoouunntteerreedd  aa  ccoouuppllee
>ooff  aaddddiittiioonnaall  pprroobblleemmss..  AA  lloott  ooff
>aapppplliiccaattiioonnss,,<BS><BS>  sseeeemm  ttoo  ppoosstt
>mmuullttiippllee  ccooppiieess  ooff  WWMM__CCHHAARR  mmeessssaaggeess
>oonnttoo  tthhee  qquueeuuee  ::--((  SSoo  ffaarr,,  II''vvee  
>-----------------------------------------------------------------

>Any smart ideas on how to get around this??

I'll bet the extra messages are 'peek's rather than 'get's.  Are you
checking for PM_REMOVE in your callback procedure?

--
Report all {*word*201} mail to your Potsmaster.

Re:Translating key messages.


Quote
David Rifkind wrote:

> I'll bet the extra messages are 'peek's rather than 'get's.  Are you
> checking for PM_REMOVE in your callback procedure?

> --
> Report all {*word*201} mail to your Potsmaster.

Oopsie... this just shows how unfamiliar I am with the subject
material... watch this space!

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Re:Translating key messages.


Quote
David Rifkind wrote:

> I'll bet the extra messages are 'peek's rather than 'get's.  Are you
> checking for PM_REMOVE in your callback procedure?

Thanks a zillion. That was exactly the problem. Seems very simple now,
and it was clearly stated in the Win32 help files, but somehow I seemed
to overlook it. That'll teach me not to skim read!

YAAY... it works brilliantly now! Grabs keystrokes very nicely from
everything apart from (oddly enough) Delphi itself! Perhaps because
Delphi is running as a de{*word*81}? Anyway... that doesn't really worry me
much :-)

MH.

--
Martin Harvey.
http://www.harvey27.demon.co.uk/mch24/
PGP key available from above address,
or http://wwwkeys.pgp.net/

Other Threads