Question

I'd like to send a keypress to a SysDateTimePick32 common control.

Imagine the following scenario: There is a table with many rows and many columns, which is user-drawn. When you hit "Enter" in one of those columns, a SysDateTimePick32 control is created and placed into the current cell so you can pick a time for this cell's actual content. This works fine, but I'd like to enable the user to start editing the time without pressing enter first.

This means: The table is in "display" mode and a cell is selected. There is no SysDateTimePick32 control, yet. Instead of pressing enter (and therefore creating and showing a SysDateTimePick32), the user types e.g. "3". Now a SysDateTimePick32 should be created and shown and the previously typed "3" should be sent to it, just like the user pressed "enter" and then "3".

I'm trying

SendMessage(sysDateTimePick32Handle, WM_KEYDOWN, '3', MAKELPARAM (1, 0));

However, this does not seem to work. What is a "clean" way to send specific keystrokes to a Win32 control, especially SysDateTimePick32?

Was it helpful?

Solution

Sending keystrokes like that is filled with bear traps. It isn't clear why it would not work, although it is the wrong way. WM_KEYDOWN is posted, not sent, so you should use PostMessage() instead. For typing keys like '3' you should send WM_CHAR instead, saves you from the hassle of getting the modifier keys state set properly (Shift, Ctrl, Alt) and removes the active keyboard layout as a failure mode. Favor SendInput() if that's not appropriate.

Do consider the less hacky way that makes this easier. Just always create the picker when the focus enters the cell. Destroy or ignore it when you find out that nothing was entered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top