SendKeys alternative that works on Citrix

后端 未结 4 1389
萌比男神i
萌比男神i 2020-12-05 08:29

I recently developed a virtual keyboard application for a customer. The program is working fine with almost all programs, but certain commands like {ENTER} or <

4条回答
  •  一个人的身影
    2020-12-05 08:41

    Try to utilize API call wia P-Invoke signature (Content edited: this is now working example - I'm sending character 'a' to the textBox, on the click of a button) :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime;
    using System.Runtime.InteropServices;
    
    namespace Test2
    {
        public partial class Form1 : Form
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct KEYBOARD_INPUT
            {
                public const uint Type = 1;
                public ushort wVk;
                public ushort wScan;
                public uint dwFlags;
                public uint time;
                public IntPtr dwExtraInfo;
            }  
    
            [StructLayout(LayoutKind.Sequential)]
            struct MOUSEINPUT
            {
                 public int dx;
                 public int dy;
                 public uint mouseData;
                 public uint dwFlags;
                 public uint time;
                 public IntPtr dwExtraInfo;
            };
    
            [StructLayout(LayoutKind.Explicit)]
            struct KEYBDINPUT 
            {
                [FieldOffset(0)]
                public ushort wVk;
                [FieldOffset(2)]
                public ushort wScan;
                [FieldOffset(4)]
                public uint dwFlags;
                [FieldOffset(8)]
                public uint time;
                [FieldOffset(12)]
                public IntPtr dwExtraInfo;
            };
    
            [StructLayout(LayoutKind.Sequential)]
            struct HARDWAREINPUT
            {
                 public uint uMsg;
                 public ushort wParamL;
                 public ushort wParamH;
            };
    
            [StructLayout(LayoutKind.Explicit)]
            struct INPUT 
            {
                 [FieldOffset(0)]
                 public int type;
                 [FieldOffset(4)]
                 public MOUSEINPUT mi;
                 [FieldOffset(4)]
                 public KEYBDINPUT ki;
                 [FieldOffset(4)]
                 public HARDWAREINPUT hi;
            };
            [DllImport("user32.dll", SetLastError = true)]
            static extern uint SendInput(uint nInputs, IntPtr pInput, int cbSize);
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                textBox1.Focus();
                INPUT Input = new INPUT();
    
                Input.type = 1;
                Input.ki.wVk = 0x41;  //ASCII for letter 'A'
                Input.ki.dwFlags = 0;  //Key is pressed down
                Input.ki.dwExtraInfo = IntPtr.Zero;
                IntPtr pInput;
                pInput = Marshal.AllocHGlobal(Marshal.SizeOf(Input));
    
                Marshal.StructureToPtr(Input, pInput, false);
                SendInput(1, pInput, Marshal.SizeOf(Input));
                Input.ki.dwFlags = 2;  //Key is released on the keyboard
    
                Marshal.StructureToPtr(Input, pInput, false);
                SendInput(1, pInput, Marshal.SizeOf(Input));
            }
        }
    }
    

提交回复
热议问题