Briefly suspending keyboard input?

From: Martin Kochanski (nothing_at_nobody.net)
Date: 03/23/04


Date: Tue, 23 Mar 2004 08:50:03 +0000

My application implements certain user commands by communicating with a
server. This means that it sends a command and waits until the server
has responded: it waits either in a message loop of its own or by
falling back into the main application message loop.

One example: when you press Ctrl+E to start editing, a request for
permission goes to the server, and the application waits for this
permission to be granted before continuing.

The user is often unaware of these delays, which are short.

My problem is that keystrokes that arrive during the "wait for server"
loop are retrieved by GetMessage before the application is properly
ready to receive them, which means (at the moment) that they are
discarded. This is not good for the user, who wants every keystroke to
go to its correct place without having to glance at the screen the whole
time.

I'm wondering whether other people have had this kind of problem and how
they have sorted it out. Essentially, I just want to suspend keyboard
input during the wait. Here are a few approaches I've considered: any
advice or comment would be welcome.

1. Cache all the WM_KEYDOWN and WM_KEYUP messages received by GetMessage
(either in GetMessage itself or using a WH_KEYBOARD hook) and
PostMessage them out after the wait has completed. One problem I can see
here is focus: some keystrokes may call up a dialog box, so I won't know
which window to post each key to. I note that keybd_event or SendInput
could be used, but is it hard to synthesize the data required by those
functions from the cached WM_KEYDOWN/UP messages?

2. Replace all occurrences of GetMessage with a pair of PeekMessage
calls that carefully avoid the keyboard range. This seems messy and I'm
not sure I'll be able to isolate every occurrence in this way.

Any other approaches I haven't considered? Any experience to share?

A sting in the tail: it would be good to allow Esc to cancel the wait
and purge any queued keystrokes.



Relevant Pages

  • Re: Something like "ping" for windows messages.
    ... the server is using CAsyncSocket but the ... Try starting the performance timer at the end of the message loop and stopping ... recorded by the timer when the next message arrives. ... This http server is already built into ...
    (microsoft.public.vc.mfc)
  • Re: Interpreting RPC extended error information: detection location 2?
    ... The GUI process is mostly retrieving information from the server ... Whenever it makes a COM call to the server, it spins its message loop ... request and receive the ORPC response, ...
    (microsoft.public.win32.programmer.ole)
  • Re: Open files with java program
    ... It runs on the client. ... keystrokes, and send data to a server. ... browser, thought he browser can launch it. ...
    (comp.lang.java.programmer)
  • Re: Multithreaded COM server problem...
    ... >>The problem is that your client code is not running a message loop. ... Nope - I meant the client. ... The server is already running such a loop ...
    (comp.lang.python)
  • RE: COMException "Unable to process message at this time"
    ... COM server is blocked, such as execute a long time task in the main thread, ... or let the main thread sleep for some time etc. ... message loop in COM Server app is blocked. ...
    (microsoft.public.dotnet.framework.interop)

Quantcast