gpt4 book ai didi

c# - Keybd_event 下划线提供错误的输出

转载 作者:太空宇宙 更新时间:2023-11-03 15:16:51 25 4
gpt4 key购买 nike

我创建了一个模拟键盘事件的小程序,用于在 Oracle 中填充数据,但是当我使用 keybd_event 引入字符下划线时,输出是错误的如果 _ 不是最后一个字符,当程序模拟下一个字符时自动添加字符 -例子我需要录音 09_80对于下划线,我使用 VK_RSHIFT 和 VK_OEM_MINUS 的组合输出为 09_-80

有人可以帮助我吗?我也尝试过使用 InputSimulator,但它只在 Windows 8 上正常运行

下面是我程序中的一些代码行,提前致谢最好的问候

//键盘操作

    [DllImport("user32.dll", SetLastError = true)]
public static extern void keybd_event(int bVk, int bScan, int dwFlags, int dwExtraInfo);

[DllImport("user32.dll")]
public static extern int MapVirtualKey(int uCode, int uMapType);

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern short VkKeyScan(char ch);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern string SendMessage(IntPtr hWnd, int msg, string wParam, IntPtr lParam);

public const int KEYEVENTF_EXTENDEDKEY = 0x0001;


public const int KEYEVENTF_KEYUP = 0x0002;

//发送字符

    public static void SendChar(char c)
{
if (bw.CancellationPending)
{
return;
}
if(c=='_')
{


keysPress(VK_RSHIFT,VK_OEM_MINUS)
}

int vk = VkKeyScan(c);
int sc = MapVirtualKey(vk, 0);
PressKeyDn(vk, sc);
ReleaseKeyUp(vk, sc);


}

//SEND STRING
public static void SendString(string s)
{

foreach (char c in s)
{
SendChar(c);
}

}

//PRESS KEY DOWN
public static void PressKeyDn(int VK_Code, int SC_Code)
{
keybd_event(VK_Code, SC_Code, 0, 0);
}

//RELEASE KEY UP
public static void ReleaseKeyUp(int VK_Code, int SC_Code)
{
keybd_event(VK_Code, SC_Code, KEYEVENTF_KEYUP, 0);
}

//Press Special Key
public static void keyPress(int VK_key)
{
if (bw.CancellationPending)
{
return;
}
int sc = MapVirtualKey(VK_key, 0);
PressKeyDn(VK_key, sc);
ReleaseKeyUp(VK_key, sc);

}

//Press Special Key Combination 2 keys
public static void keysPress(int VK_Key1,int VK_Key2)
{
if (bw.CancellationPending)
{
return;
}
int sc1 = MapVirtualKey(VK_Key1, 0);
int sc2 = MapVirtualKey(VK_Key2, 0);
PressKeyDn(VK_Key1, sc1);
PressKeyDn(VK_Key2, sc2);
ReleaseKeyUp(VK_Key2, sc2);
ReleaseKeyUp(VK_Key1, sc1);

}

//Press Special Key Combination 3 keys
public static void keysPress(int VK_Key1, int VK_Key2,int VK_Key3)
{
if (bw.CancellationPending)
{
return;
}
int sc1 = MapVirtualKey(VK_Key1, 0);
int sc2 = MapVirtualKey(VK_Key2, 0);
int sc3 = MapVirtualKey(VK_Key3, 0);
PressKeyDn(VK_Key1, sc1);
PressKeyDn(VK_Key2, sc2);
PressKeyDn(VK_Key3, sc3);
ReleaseKeyUp(VK_Key3, sc3);
ReleaseKeyUp(VK_Key2, sc2);
ReleaseKeyUp(VK_Key1, sc1);
}

最佳答案

好的,经过一些测试,我发现了问题所在,并已修复。_(下划线)表示第二行语句的延续,因此下一个字符以 - 开头。我已经解决了这个问题,通过这种方式从方法中排除了下一个字符。

public static void SendChar(char c)
{
bool und = false;
if (c == '_')
{
und = true;
}
int vk = BMDLCommands.VkKeyScan(c);
int sc = BMDLCommands.MapVirtualKey(vk, 0);
if(und)
{
keysPress(VK_SHIFT, VK_OEM_MINUS);
MethodInvoker met = delegate
{
keyPress(vk);
};
}
else
{
BMDLCommands.PressKeyDn(vk, sc);
BMDLCommands.ReleaseKeyUp(vk, sc);
}
}

现在输出完全正确。非常感谢您的帮助

关于c# - Keybd_event 下划线提供错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38771514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com