How To Create lParam Of SendMessage WM_KEYDOWN

为君一笑 提交于 2021-02-17 23:59:26

问题


I'm trying to use SendMessage to send a keystroke, and don't really understand the lParam. I understand that the different bits represent each parameter and that they need to be arranged in order.

I've read this question & this, so I know which order the bits need to be in, I just don't know how to do it...

How would I create the following lParam?

repeat cound = 0,
scan code = {Don't know what this is?},
extended key = 1,
reserved = 0,
context code = 0,
previous key state = 1,
transition state = 0

回答1:


I realized that AutoIT has the functionality that I need, so have looked at the source file sendKeys.cpp and found the following C++ code snippet for this function, it will be easy enough to translate into C#:

scan = MapVirtualKey(vk, 0);

// Build the generic lparam to be used for WM_KEYDOWN/WM_KEYUP/WM_CHAR
lparam = 0x00000001 | (LPARAM)(scan << 16);         // Scan code, repeat=1
if (bForceExtended == true || IsVKExtended(vk) == true)
    lparam = lparam | 0x01000000;       // Extended code if required

if ( (m_nKeyMod & ALTMOD) && !(m_nKeyMod & CTRLMOD) )   // Alt without Ctrl
    PostMessage(m_hWnd, WM_SYSKEYDOWN, vk, lparam | 0x20000000);    // Key down, AltDown=1
else
    PostMessage(m_hWnd, WM_KEYDOWN, vk, lparam);    // Key down

The scan code can be generated with MapVirtualKey

C# Translation:

public static void sendKey(IntPtr hwnd, VKeys keyCode, bool extended)
{
    uint scanCode = MapVirtualKey((uint)keyCode, 0);
    uint lParam;

    //KEY DOWN
    lParam = (0x00000001 | (scanCode << 16));
    if (extended)
    {
        lParam |= 0x01000000;
    }
    PostMessage(hwnd, (UInt32)WMessages.WM_KEYDOWN, (IntPtr)keyCode, (IntPtr)lParam);

    //KEY UP
    lParam |= 0xC0000000;  // set previous key and transition states (bits 30 and 31)
    PostMessage(hwnd, WMessages.WM_KEYUP, (uint)keyCode, lParam);
}

[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);



回答2:


If you absolutely have to use SendMessage, then you need to toggle the bits of your int at the correct positions.

This site documents how to do this in C#:

http://codeidol.com/csharp/csharpckbk2/Classes-and-Structures/Turning-Bits-On-or-Off/

Referring to your question, ScanCode is the value of the Key that you're trying to send and represents certain states too. For example the scan code for pressing A is different to the code for releasing A.

Wikipedia has an article on them:

http://en.wikipedia.org/wiki/Scancode



来源:https://stackoverflow.com/questions/10280000/how-to-create-lparam-of-sendmessage-wm-keydown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!