NO

Author Topic: hooks don't work  (Read 2945 times)

rko

  • Guest
hooks don't work
« on: March 25, 2015, 12:51:15 AM »
hi,
i try to translate a pascal program to learn about c.
i try to use hooks (keyboard) and they don't work in the program after compiling for 64 bit.
can anybody spot the problem and offer some hints?
thanks in advance

colepc

  • Guest
Re: hooks don't work
« Reply #1 on: April 05, 2015, 05:48:56 PM »
I found this code in your wingraphics.c:
   case WM_CHAR:
   case WM_KEYDOWN:
   case WM_SYSKEYDOWN:
      if( KeyboardHook  != NULL ) {
         return (int) KeyboardHook(grHandle,mess,wParam,lParam);
      }
      return 0;
   case WM_SYSCHAR:
      return 0; /*this message is inserted TO avoid keyboard beep*/

You use WM_CHAR, WM_KEYDOWN, and WM_SYSKEYDOWN for different purposes.

Use WM_CHAR for your normal alpha-numeric keys. For example:
case WM_CHAR:
  if(wParam>='0' && wParam<='9')
 {
   do something with numeric keys
   return 0;
 }

Use WM_KEYDOWN for special keys such as F1 and Home. For example:
case WM_KEYDOWN:
  if(wParam==VK_F1) //F1 key
 {
  do something with the F1 key
  return 0;
 }

Use WM_SYSKEYDOWN for special keys such as Alt and F10. For example:
case WM_SYSKEYDOWN:
  if(wParam==VK_MENU)
  {
    do something with the Alt key
    return 0;
  }

Try searching for "Using Keyboard Input" and "Virtual Key Codes". Microsoft has a lot of useful information you can use for your research.


colepc

  • Guest
Re: hooks don't work
« Reply #2 on: April 08, 2015, 01:16:42 AM »
In order to understand what WM_CHAR, WM_KEYDOWN, and WM_SYSKEYDOWN do, you could write a simple program that writes information to the screen when those "cases" are triggered. For example, when Ctrl C is pushed my program shows something like this (events scroll up - the bottom has column descriptions):

WM_KEYDOWN  | 0x11   |00000000001D0001
WM_KEYDOWN  |0x43 'C'|00000000002E0001
WM_CHAR    |0x03    |00000000002E0001
--------------------------------------------------------------
Where from   | wParm | lParam

As you can see from this, the wParm parameter has useful information about the events. The Ctrl key produces 0x11 in WM_KEYDOWN, then the 'C' key produces 0x43 in WM_KEYDOWN, and finally the combination of the two keys produces the expected 0x03 in WM_CHAR (just don't hold the Ctrl key for too long or it will repeat). I was interested in creating "characters" using two digit hexadecimal so I used the key program to come up with a concept. The concept I came up with is to push and release the Alt key followed by pushing two hex characters. For example, Alt 31 produces the numeric character 1 and Alt ff produces a color coded 'F'.

Note: If you have the book "Programming Windows Fifth Edition" by Charles Petzold, he has already developed programs that give key information similar to above when keys are pushed (see Chapter 6 "KeyView1" and "KeyView2" - they are also on the book's CD).


rko

  • Guest
Re: hooks don't work
« Reply #3 on: April 08, 2015, 03:10:51 PM »
thank you so much.
i will try your suggestion.