如何创建lParam的SendMessage WM_KEYDOWN [英] How To Create lParam Of SendMessage WM_KEYDOWN

查看:185
本文介绍了如何创建lParam的SendMessage WM_KEYDOWN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SendMessage 发送一个击键,并且不太了解lParam.我知道不同的位代表每个参数,它们需要按顺序排列.

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...

如何创建以下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

推荐答案

我意识到 AutoIT 具有以下功能:我需要,所以已经看过文件sendKeys.cpp并找到此功能的以下C ++代码段,将其轻松转换为C#:

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

可以使用 MapVirtualKey

C#翻译:

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);

这篇关于如何创建lParam的SendMessage WM_KEYDOWN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆