On how to do nothing else but to call it

by Michael S. Kaplan, published on 2007/02/08 06:01 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2007/02/08/1623919.aspx


Mike Akin asked in regard to the ClearKeyboardBuffer routine I had discussed previously in the Getting all you can out of a keyboard layout series:

What would i have to do to convert this function so that I don't need to do anything else but call it?

Thanks,

Mike

I admit I was kind of baffled at first trying to understand what Mike was asking. I am going to try and use this post to figure it out. :-)

We'll start with the code, first introduced in Part 7 of the series:

private static void ClearKeyboardBuffer(uint vk, uint sc, IntPtr hkl) {
    StringBuilder sb = new StringBuilder(10);
    int rc = 0;
    while(rc != 1) {
        rc = ToUnicodeEx(vk, sc, lpKeyStateNull, sb, sb.Capacity, 0, hkl);
    }

Perhaps he meant he did not want to pass a VK value, since (as I mentioned there) you would want to be especially careful to pick one that is unlikely to ever have any kind of dead key assignment -- which is why i was choosing VK_DECIMAL.

Of course the function could just call it with VK_DECIMAL explicitly and not require the caller to pass it.

Or maybe he meant he did not want to pass in the target keyboard (that hkl parameter). Even easier -- remove the parameter and call ToUnicode rather than calling ToUnicodeEx. :-)

Or perhaps Mike was trying to not make it be a C# function but instead put it in another language. This too is easy enough, though of course the answer would depend on the language in question....

Maybe what was actually meant was getting rif of the external dependency caused by that lpKeyStaeNull value. You could replace the lpKeyStateNull with what was assigned to that variable (just an empty key state array).

Those are my guesses about the intent. Mike, can you clarify which you meant (or give me the specifics of the one I missed?).

 

This post brought to you by (U+2400, a.k.a. SYMBOL FOR NULL)


no comments

Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

go to newer or older post, or back to index or month or day