简体   繁体   中英

Determine whether WM_KEYDOWN's wParam is printable

I am trying to detect non-printable characters in my window's WM_KEYDOWN message, but research so far has yielded no results. I have tried the following methods:

  • 1: iscntrl(wParam) , which does not work for the arrow keys
  • 2: ischar(wParam) , which also does not work for the same keys
  • 3: ToAscii(wParam, MapVirtualKey(wParam, 0) ...) , which still does not work

IMO the third method should work because arrow keys are not on any ASCII or Unicode tables.

The thing is I would like to send to my event handlers both the keycode and the character. Since WM_CHAR is posted after WM_KEYDOWN , I would like to just send the key in WM_KEYDOWN if it is not printable (because WM_CHAR would not be sent to the window so I cannot do it there).

Thanks

Given that most of the keys which are generally considered "printable" are contiguous, why not just use a few if statements to decide how you want to handle them? In WM_KEYDOWN, decide whether a key is "printable" or not, and if it is, fire it off to WM_CHAR.

The functions that you're researching aren't designed for this specific purpose, so ironically, you might spend more time researching them than it would take to make an ad-hoc solution on your own.

Your method #3 should work for this. In PreTranslateMessage , handling WM_KEYDOWN :

BYTE keyboardState[256];
::GetKeyboardState(keyboardState);
WORD ascii;
int len = ::ToAscii(wParam, (lParam >> 16) & 0xFF, keyboardState, &ascii, 0);
if (len == 1)
{
    /* it's printable */
    printf("%c", ascii & 0xFF); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM