Monday, March 10, 2008

SetTimer() inside a thread - use SetTimer API method instead and pass a callback

CWinThread does not have a SetTimer method. So you can not use

BEGIN_MESSAGE_MAP(CArc220Thread, CWinThread)
ON_WM_TIMER()
END_MESSAGE_MAP()

or

myThreadPointer->SetTimer(1, 500, NULL);

Use the SetTimer API method instead and pass a callback. Make sure the HWND parameter is NULL.

============ from the thread's .h file ============

// NOTE: this is NOT declared inside the class

void CALLBACK EXPORT processArc220Data(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
);

============ from the thread's .cpp file =============

timerValue = ::SetTimer(NULL, IDT_ARC220_TIMER, 1000, (TIMERPROC) processArc220Data);
.
.
.
void CALLBACK processArc220Data(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
)
.
.
.
::KillTimer(NULL, timerValue);




Keybd_event in Compact Framework

keybd_event(VkKeyScan(‘A’),0x9e,0 , 0); // ‘A’ Press

keybd_event(VkKeyScan(‘A’),0x9e, KEYEVENTF_KEYUP,0); // ‘A’ Release

But, you can not use VkKeyScan in Window CE. So you have to adjust it based on previous
two articles: Virtual Key codes as following:

keybd_event(0x41,0x9e,0 , 0); // ‘A’ Press

keybd_event(
0x41
,0x9e, KEYEVENTF_KEYUP,0); // ‘A’ Release




Virtual Key Codes

Virtual Key Codes
Symbolic constant names, hexadecimal values, and keyboard equivalents for the virtual key codes used by Windows.

Windows defines special constants for each key the user can press. The virtual-key codes identify various virtual keys. These constants can then be used to refer to the keystroke when using Delphi and Windows API calls or in an OnKeyUp or OnKeyDown event handler. Virtual keys mainly consist of actual keyboard keys, but also include "virtual" elements such as the three mouse buttons. Delphi defines all constants for Windows virtual key codes in the Windows unit.

Here are some of the Delphi articles that deal with the keyboard and VK codes:
· Keyboard Symphony
Delphi For Beginners: Get familiar with the OnKeyDown, OnKeyUp, and onKeyPress event procedures to respond to various key actions or handle and process ASCII characters along with other special purpose keys.
· How to Translate a Virtual Key Code to a Character
Windows defines special constants for each key the user can press. The virtual-key codes identify various virtual keys. In Delphi, the OnKeyDown and OnKeyUp events provide the lowest level of keyboard response. To use OnKeyDown or OnKeyUp to test for keys the user presses, you must use Virtual key codes to get the key pressed. Here's how to translate the virtual key code to the corresponding Windows character.
· Touch Me - I'm Untouchable
Intercepting keyboard input for controls that cannot receive the input focus. Working with keyboard hooks from Delphi.
· ENTERing TAB
Using the Enter key like a Tab key with Delphi controls.
· Abort a Loop by Pressing a Key
Use the VK_ESCAPE to abort a (for) loop.
· Use Arrow Keys to Move Between Controls
The UP and DOWN arrow keys are virtually useless in edit controls. So why not use them for navigating between fields.
· Shift, Ctrl, Alt Key Checking
Using virtual key codes to check the key state.
· Simulating keystrokes from code
A handy function to simulate the pressing of keyboard keys.

The following table shows the symbolic constant names, hexadecimal values, and keyboard equivalents for the virtual-key codes used by Windows. Some Windows 2000 and OEM specific constants are missing, the entire list is available from Microsoft. The codes are listed in numerical order.

Symbolic
constant name
Value
(hexadecimal)
Keyboard (or mouse) equivalent
VK_LBUTTON 01 Left mouse button
VK_RBUTTON 02 Right mouse button
VK_CANCEL 03 Control-break processing
VK_MBUTTON 04 Middle mouse button (three-button mouse)
VK_BACK 08 BACKSPACE key
VK_TAB 09 TAB key
VK_CLEAR 0C CLEAR key
VK_RETURN 0D ENTER key
VK_SHIFT 10 SHIFT key
VK_CONTROL 11 CTRL key
VK_MENU 12 ALT key
VK_PAUSE 13 PAUSE key
VK_CAPITAL 14 CAPS LOCK key
VK_ESCAPE 1B ESC key
VK_SPACE 20 SPACEBAR
VK_PRIOR 21 PAGE UP key
VK_NEXT 22 PAGE DOWN key
VK_END 23 END key
VK_HOME 24 HOME key
VK_LEFT 25 LEFT ARROW key
VK_UP 26 UP ARROW key
VK_RIGHT 27 RIGHT ARROW key
VK_DOWN 28 DOWN ARROW key
VK_SELECT 29 SELECT key
VK_PRINT 2A PRINT key
VK_EXECUTE 2B EXECUTE key
VK_SNAPSHOT 2C PRINT SCREEN key
VK_INSERT 2D INS key
VK_DELETE 2E DEL key
VK_HELP 2F HELP key

30 0 key

31 1 key

32 2 key

33 3 key

34 4 key

35 5 key

36 6 key

37 7 key

38 8 key

39 9 key

41 A key

42 B key

43 C key

44 D key

45 E key

46 F key

47 G key

48 H key

49 I key

4A J key

4B K key

4C L key

4D M key

4E N key

4F O key

50 P key

51 Q key

52 R key

53 S key

54 T key

55 U key

56 V key

57 W key

58 X key

59 Y key

5A Z key
VK_NUMPAD0 60 Numeric keypad 0 key
VK_NUMPAD1 61 Numeric keypad 1 key
VK_NUMPAD2 62 Numeric keypad 2 key
VK_NUMPAD3 63 Numeric keypad 3 key
VK_NUMPAD4 64 Numeric keypad 4 key
VK_NUMPAD5 65 Numeric keypad 5 key
VK_NUMPAD6 66 Numeric keypad 6 key
VK_NUMPAD7 67 Numeric keypad 7 key
VK_NUMPAD8 68 Numeric keypad 8 key
VK_NUMPAD9 69 Numeric keypad 9 key
VK_SEPARATOR 6C Separator key
VK_SUBTRACT 6D Subtract key
VK_DECIMAL 6E Decimal key
VK_DIVIDE 6F Divide key
VK_F1 70 F1 key
VK_F2 71 F2 key
VK_F3 72 F3 key
VK_F4 73 F4 key
VK_F5 74 F5 key
VK_F6 75 F6 key
VK_F7 76 F7 key
VK_F8 77 F8 key
VK_F9 78 F9 key
VK_F10 79 F10 key
VK_F11 7A F11 key
VK_F12 7B F12 key
VK_F13 7C F13 key
VK_F14 7D F14 key
VK_F15 7E F15 key
VK_F16 7F F16 key
VK_F17 80H F17 key
VK_F18 81H F18 key
VK_F19 82H F19 key
VK_F20 83H F20 key
VK_F21 84H F21 key
VK_F22 85H F22 key
VK_F23 86H F23 key
VK_F24 87H F24 key
VK_NUMLOCK 90 NUM LOCK key
VK_SCROLL 91 SCROLL LOCK key
VK_LSHIFT A0 Left SHIFT key
VK_RSHIFT A1 Right SHIFT key
VK_LCONTROL A2 Left CONTROL key
VK_RCONTROL A3 Right CONTROL key
VK_LMENU A4 Left MENU key
VK_RMENU A5 Right MENU key
VK_PLAY FA Play key
VK_ZOOM FB Zoom key

http://delphi.about.com/od/objectpascalide/l/blvkc.htm

Keyboard Events Simulation using keybd_event() function



http://www.codeproject.com/KB/system/keyboard.aspx

Introduction

Simulation of a keyboard input is a well known concept for those who are all familiar with Visual Basic. SendKeys() in Visual Basic does all the things, if you want to do anything without keyboard. But what is in SendKeys() function? What does it do? Can we do such a thing with Visual C++? This article is the answer. I think this will be useful for beginners who are all just trying to do something different with VC++. Let us get into the steps…

Function Syntax

void keybd_event(BYTE bVirtualKey, BYTE bScanCode,
DWORD dwFlags, DWORD dwExtraInfo);
  • bVirtualKey //Virtual Keycode of keys. E.g., VK_RETURN, VK_TAB…
  • bScanCode //Scan Code value of keys. E.g., 0xb8 for “Left Alt” key.
  • dwFlags //Flag that is set for key state. E.g., KEYEVENTF_KEYUP.
  • dwExtraInfo //32-bit extra information about keystroke.

Function Details:

  • bVirtualKey

    Virtual keycode that has to be send as key input. The following are the available predefined virtual key codes:

    VK_NUMPAD7 0x67 VK_BACK 0x08
    VK_NUMPAD8 0x68 VK_TAB 0x09
    VK_NUMPAD9 0x69 VK_RETURN 0x0D
    VK_MULTIPLY 0x6A VK_SHIFT 0x10
    VK_ADD 0x6B VK_CONTROL 0x11
    VK_SEPARATOR 0x6C VK_MENU 0x12
    VK_SUBTRACT 0x6D VK_PAUSE 0x13
    VK_DECIMAL 0x6E VK_CAPITAL 0x14
    VK_DIVIDE 0x6F VK_ESCAPE 0x1B
    VK_F1 0x70 VK_SPACE 0x20
    VK_F2 0x71 VK_END 0x23
    VK_F3 0x72 VK_HOME 0x24
    VK_F4 0x73 VK_LEFT 0x25
    VK_F5 0x74 VK_UP 0x26
    VK_F6 0x75 VK_RIGHT 0x27
    VK_F7 0x76 VK_DOWN 0x28
    VK_F8 0x77 VK_PRINT 0x2A
    VK_F9 0x78 VK_SNAPSHOT 0x2C
    VK_F10 0x79 VK_INSERT 0x2D
    VK_F11 0x7A VK_DELETE 0x2E
    VK_F12 0x7B VK_LWIN 0x5B
    VK_NUMLOCK 0x90 VK_RWIN 0x5C
    VK_SCROLL 0x91 VK_NUMPAD0 0x60
    VK_LSHIFT 0xA0 VK_NUMPAD1 0x61
    VK_RSHIFT 0xA1 VK_NUMPAD2 0x62
    VK_LCONTROL 0xA2 VK_NUMPAD3 0x63
    VK_RCONTROL 0xA3 VK_NUMPAD4 0x64
    VK_LMENU 0xA4 VK_NUMPAD5 0x65
    VK_RMENU 0xA5 VK_NUMPAD6 0x66

    Character key can be converted into virtual key using VkKeyScan(TCHAR ch) function.

  • bScanCode

    Scan code is the hardware key code for the key (make and break codes). The following are the available scan codes (break code will be used in this parameter).

  • dwFlags

    A set of flag bits that specify various aspects of function operation. An application can use any combination of the following predefined constant values to set the flags.

    Value Meaning
    KEYEVENTF_EXTENDEDKEY If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
    KEYEVENTF_KEYUP If specified, the key is being released. If not specified, the key is being depressed.
  • dwExtraInfo

    32-bit extra information along with the keyboard input.

Example Code

// Simulating a Alt+Tab keystroke

keybd_event(VK_MENU,0xb8,0 , 0); //Alt Press

keybd_event(VK_TAB,0x8f,0 , 0); // Tab Press

keybd_event(VK_TAB,0x8f, KEYEVENTF_KEYUP,0); // Tab Release

keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0); // Alt Release


// Simulating a Ctrl+A keystroke

keybd_event(VK_CONTROL,0x9d,0 , 0); // Ctrl Press

keybd_event(VkKeyScan(‘A’),0x9e,0 , 0); // ‘A’ Press

keybd_event(VkKeyScan(‘A’),0x9e, KEYEVENTF_KEYUP,0); // ‘A’ Release

keybd_event(VK_CONTROL,0x9d,KEYEVENTF_KEYUP,0); // Ctrl Release

Thursday, March 6, 2008

Windows CE Keyboard Hook (Error 31)

http://msdn.microsoft.com/chats/transcripts/mobileembedded/06_0830_msdn_wince.aspx


PaulT_MVP (Expert):
Q: I have two .NET EXEs both installing a WH_KEYBOARD_LL keyboard hook on Windows Mobile 5 Smartphone and the second EXE fails to create it's hook; GetLastError returns 31, which means "A device attached to the system is not functioning." What is going on?
A: Rob, I just tried this with two *unmanaged* EXEs and the second gets NULL back from SetWindowsHookEx, also. I think that having two keyboard hooks must not be supported.

Pablo Candelas Gonzalez (Expert):
Q: Exactly, the 2nd SetWindowsHookEx returns 0, with GetLastError returning 31. How can it be the case that two separate .NET exes cannot install a keyboard hook? Think about that... two apps that have nothing to do with each other prevent the other app from creating the hook. horrible, eh? There must be a solution?
A: About SetWindowsHookExW, I am pretty sure GWES only supports one Keyboard Hook and not more… this is cached, thus the second time you try to call this API it will return the not so useful ERROR_GEN_FAILURE. Sorry for the unfortunate limitation, but that is the way it is today.

Windows CE Keyboard Hook

Windows CE Keyboard Hook


http://weibeltech.com/?p=110

A teammate needed to write a Windows CE Keyboard Hook. So I gave them a hand, after doing a little digging I came across this posting. Many thanks to Prathamesh S Kulkarni for this post.

http://www.codeproject.com/ce/wincekbhook.asp?print=true
I duplicated the core of this sample code below so I don’t loose it in the future. Check out Prathamesh’s code for commented code. To make this work call activate() at the beginning of your app.
if (!activate(hInst, hook)) {
return FALSE;
}
And deactivate() at the end.
deactivate();
Then fill in the hook() with some interesting logic.

#include 
#include
#include

#define WH_KEYBOARD_LL 20

typedef LRESULT (CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);
typedef HHOOK (WINAPI *_SetWindowsHookExW)(int, HOOKPROC, HINSTANCE, DWORD);
typedef LRESULT (WINAPI *_CallNextHookEx)(HHOOK, int, WPARAM, LPARAM);
typedef LRESULT (WINAPI *_UnhookWindowsHookEx)(HHOOK);

typedef struct {
DWORD vkCode;
DWORD scanCode;
DWORD flags;
DWORD time;
ULONG_PTR dwExtraInfo;
} KBDLLHOOKSTRUCT, *PKBDLLHOOKSTRUCT;

static _SetWindowsHookExW SetWindowsHookEx;
static _UnhookWindowsHookEx UnhookWindowsHookEx;
static _CallNextHookEx CallNextHookEx;

HINSTANCE gHook = NULL;
HHOOK gInst = NULL;

BOOL activate(HINSTANCE, HOOKPROC);
BOOL deactivate();
LRESULT CALLBACK hook(int nCode, WPARAM wParam, LPARAM lParam);

//
// activate() -
//
BOOL
activate(HINSTANCE hInst, HOOKPROC hProc)
{
SetWindowsHookEx = NULL;
CallNextHookEx = NULL;
UnhookWindowsHookEx = NULL;

gHook = LoadLibrary(_T("coredll.dll"));
if(gHook == NULL) {
return(false);
} else {

SetWindowsHookEx = (_SetWindowsHookExW)GetProcAddress(
gHook, _T("SetWindowsHookExW"));
if(SetWindowsHookEx == NULL) {
return(false);
} else {
gInst = SetWindowsHookEx(WH_KEYBOARD_LL, hProc, hInst, 0);
if(gInst == NULL) {
return false;
}
}

CallNextHookEx = (_CallNextHookEx)GetProcAddress(
gHook, _T("CallNextHookEx"));
if(CallNextHookEx == NULL) {
return false;
}

UnhookWindowsHookEx = (_UnhookWindowsHookEx)GetProcAddress(
gHook, _T("UnhookWindowsHookEx"));
if(UnhookWindowsHookEx == NULL) {
return(false);
}
}
return(true);
}


//
// deactivate() -
//
BOOL
deactivate()
{
if(gInst != NULL) {
UnhookWindowsHookEx(gInst);
gInst = NULL;
}
if(gHook != NULL) {
FreeLibrary(gHook);
gHook = NULL;
}
return(true);
}

//
// hook() -
//
LRESULT CALLBACK
hook(int nCode, WPARAM wParam, LPARAM lParam)
{
// Do something interesting here
return CallNextHookEx(gInst, nCode, wParam, lParam);
}