Please refer to the screenshot below:
The datetime control is a Compact Framework DateTimePicker, the numbered buttons are stndard Button controls.
Clicking on the arrow of the DateTimePicker displays a calendar control allowing the suer to select a date. However if the user clicks on any part of the selected Text date it is highlighted and can be amended using either the hard keys or the on-screen keyboard. I'm trying to emulate this functionality with my standard buttons.
I've have tried the following but currently this is only causing the DateTimePicker to lose focus.
const int KEYEVENTF_KEYUP = 0x2;
const int KEYEVENTF_KEYDOWN = 0x0;
const int VK_NUMPAD1 = 0x61;
private void digitButton_Clicked(object sender, EventArgs e)
{
keybd_event(VK_NUMPAD1, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(VK_NUMPAD1, 0, KEYEVENTF_KEYUP, 0);
}
[DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event
(
byte bVk,
byte bScan,
int dwFlags,
int dwExtraInfo
);
I think you pointed out your problem, "...but currently this is only causing the DateTimePicker to lose focus". I suspect your buttons are gaining focus and as a result are consuming the keystrokes. You might try setting focus to the DateTimePicker before generating the keystrokes. If that fails, as a last resort you can hard code to the control using SendMessage() to the window handle for the DateTimePicker, with WM_KEYDOWN, WM_KEYUP.
Try the following - while I haven't tested it for your exact scenario, it does work in our .NET CF applications:
const int KEYEVENTF_EXTENDED_KEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
const int KEYEVENTF_KEYDOWN = 0x0;
const int VK_NUMPAD1 = 0x61;
private void digitButton_Clicked(object sender, EventArgs e)
{
keybd_event(VK_NUMPAD1, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_NUMPAD1, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
[DllImport("coredll.dll", EntryPoint = "keybd_event", SetLastError = true)]
public static extern void keybd_event
(
byte bVk,
byte bScan,
int dwFlags,
int dwExtraInfo
);
keybd_event works fine, BUT you have to focus the datePicker first:
(on a form with one datePicker and 12 buttons (button1 to button12)):
public Form1()
{
InitializeComponent();
foreach (Control c in this.Controls)
{
if (c is Button)
{
int n = getButtonNumber((Button)c);
if (n <= 10)
c.Text = n.ToString();
c.Click += new EventHandler(c_Click);
}
}
button11.Text = "<-"; button12.Text = "OK";
}
void c_Click(object sender, EventArgs e)
{
if (sender is Button)
{
Button b = (Button)sender;
int n = Convert.ToInt16( b.Text);
digitButtonClick(n);
}
}
private void digitButtonClick(int n)
{
byte vkInt = (byte)(0x30 + n);
dateTimePicker1.Focus();
keybd_event(vkInt, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(vkInt, 0, KEYEVENTF_KEYUP, 0);
}
regards
Josef
Related
I am creating a winforms application in visual studio 2017, I am populating the list box using a List.
I set the multicolumn property to true. Since I have lots of strings in my list, there is a horizontal scrollbar appearing at the bottom of the box.
The application I am creating should be working on a tablet, so therefore the scroll bar is not easy to navigate using fingers.
My question is, is there a way to be able to control this scrollbar using a button ?
Yes, It is possible to control the behavior you are expecting with the help of Buttons.
To move from right to left -
private void btnLeft_Click(object sender, EventArgs e)
{
int visibleItemsInColumn = listBox1.ClientSize.Height / listBox1.ItemHeight; //No of items in each column. In this case - 5
listBox1.TopIndex = listBox1.TopIndex - visibleItemsInColumn;
}
To move from left to right -
private void btnRight_Click(object sender, EventArgs e)
{
int visibleItemsInColumn = listBox1.ClientSize.Height / listBox1.ItemHeight;
listBox1.TopIndex = listBox1.TopIndex + visibleItemsInColumn;
}
What it actually does is every time you click on button, It
increases/decreases the TopIndex by the total elements per columns. So
on each clicks, you move one column either left or right.
You can send WM_HSCROLL message to the ListBox to scroll it. To do so, you should first get the scroll position by calling GetScrollInfo methods.
The following code, scrolls the ListBox, 1 column to the right:
var info = new SCROLLINFO() { fMask = ScrollInfoMask.SIF_ALL };
GetScrollInfo(listBox1.Handle, SBOrientation.SB_HORZ, ref info);
var wparam = ((uint)(info.nPos + 1) << 16) | (SB_THUMBPOSITION & 0xffff);
SendMessage(listBox1.Handle, WM_HSCROLL, wparam, 0);
To scroll one column to the left, use info.nPos - 1.
Here are the declarations which you need:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg,
uint wParam, uint lParam);
[StructLayout(LayoutKind.Sequential)]
struct SCROLLINFO {
public uint cbSize;
public ScrollInfoMask fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
public enum ScrollInfoMask : uint {
SIF_RANGE = 0x1,
SIF_PAGE = 0x2,
SIF_POS = 0x4,
SIF_DISABLENOSCROLL = 0x8,
SIF_TRACKPOS = 0x10,
SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS),
}
[DllImport("user32.dll")]
private static extern bool GetScrollInfo(IntPtr hwnd,
SBOrientation fnBar, ref SCROLLINFO lpsi);
public enum SBOrientation : int {
SB_HORZ = 0x0,
SB_VERT = 0x1,
}
const uint WM_HSCROLL = 0x0114;
const uint SB_THUMBPOSITION = 4;
I am using InputSimulator to simulate key presses and mouse clicks. Every key I've tested so far works, except the mouse buttons. I send them like this:
private void button2_Click(object sender, EventArgs e) //In this example I am trying to simulate the left mouse button
{
System.Threading.Thread.Sleep(2000);
InputSimulator.SimulateKeyPress(VirtualKeyCode.LBUTTON);
}
But nothing happens. Do I do anything wrong?
Library: InputSimulator
The latest version of InputSimulator supports mouse events. Here is how to use it:
var sim = new InputSimulator();
sim.Mouse.LeftButtonClick();
Note that binary download is outdated, so you have to build the library from sources.
I dont know much about InputStimulator but according to this post. You can stimulate mouse click using;
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
private const int MOUSEEVENTF_MOVE = 0x0001;
private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
private const int MOUSEEVENTF_RIGHTUP = 0x0010;
private const int MOUSEEVENTF_WHEEL = 0x0800;
private const int MOUSEEVENTF_XDOWN = 0x0080;
private const int MOUSEEVENTF_XUP = 0x0100;
//.................................
//In your own function:
int X = 1220;
int Y = 13;
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, X, Y, 0, 0);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
Remember to add using System.Runtime.InteropService;
Here is my code that I use to simulate a tab-keypress in a certain process:
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
public Form1()
{
PostMessage(MemoryHandler.GetMainWindowHandle(),
(int)KeyCodes.WMessages.WM_KEYDOWN,
(int)KeyCodes.VKeys.VK_TAB, 0);
InitializeComponent();
}
Is there any way to extend it so that it presses the key for (example) 1 second, instead of just tapping it?
Please note that I'm not interested in a Thread.Sleep() solution that blocks the UI thread.
The repeating of a keystroke when you hold it down is a feature that's built into the keyboard controller. A microprocessor that's built into the keyboard itself. The 8042 microcontroller was the traditional choice, the keyboard device driver still carries its name.
So, no, this is not done by Windows and PostMessage() is not going to do it for you. Not exactly a problem, you can simply emulate it with a Timer.
If you want to simulate what Windows does with messages, you likely want to find out how fast the key repeat rate is. That can be found at HKEY_CURRENT_USER\Control Panel\Keyboard\KeyboardSpeed. there's also the KeyboardDelay value.
What Windows does is send a WM_KEYDOWN and a WM_CHAR when a key is initially pressed. Then, if the key is still pressed after KeyboardDelay time span, the WM_KEYDOWN and WM_CHAR pair are repeated every KeyboardSpeed until the key is depressed--at which point WM_KEYUP is sent.
I would suggest using a Timer to send the messages at a specific frequencies.
Update:
for example:
int keyboardDelay, keyboardSpeed;
using (var key = Registry.CurrentUser.OpenSubKey(#"Control Panel\Keyboard"))
{
Debug.Assert(key != null);
keyboardDelay = 1;
int.TryParse((String)key.GetValue("KeyboardDelay", "1"), out keyboardDelay);
keyboardSpeed = 31;
int.TryParse((String)key.GetValue("KeyboardSpeed", "31"), out keyboardSpeed);
}
maxRepeatedCharacters = 30; // repeat char 30 times
var startTimer = new System.Windows.Forms.Timer {Interval = keyboardSpeed};
startTimer.Tick += startTimer_Tick;
startTimer.Start();
var repeatTimer = new System.Windows.Forms.Timer();
repeatTimer.Interval += keyboardDelay;
repeatTimer.Tick += repeatTimer_Tick;
//...
private static void repeatTimer_Tick(object sender, EventArgs e)
{
PostMessage(MemoryHandler.GetMainWindowHandle(),
(int)KeyCodes.WMessages.WM_KEYDOWN,
(int)KeyCodes.VKeys.VK_TAB, 0);
PostMessage(MemoryHandler.GetMainWindowHandle(),
(int)KeyCodes.WMessages.WM_CHAR,
(int)KeyCodes.VKeys.VK_TAB, 0);
counter++;
if (counter > maxRepeatedCharacters)
{
Timer timer = sender as Timer;
timer.Stop();
}
}
private static void startTimer_Tick(object sender, EventArgs eventArgs)
{
Timer timer = sender as Timer;
timer.Stop();
PostMessage(MemoryHandler.GetMainWindowHandle(),
(int)KeyCodes.WMessages.WM_KEYDOWN,
(int)KeyCodes.VKeys.VK_TAB, 0);
PostMessage(MemoryHandler.GetMainWindowHandle(),
(int)KeyCodes.WMessages.WM_CHAR,
(int)KeyCodes.VKeys.VK_TAB, 0);
}
When holding a key down on a physical keyboard, repeated keystrokes are passed to the active window. This is not built into the keyboard, but is a Windows feature.
You can simulate this by doing the following steps in order:
Send a keydown message.
Run a timer at 30 ms intervals (the default in Windows, changeable through Ease of Access settings), sending a keypress message to the window at each tick.
Send a keyup message.
I would do it in a thread, for sleeping and not-blocking the UI thread.
Look at this:
System.Threading.Thread KeyThread = new System.Threading.Thread(() => {
//foreach process
// press key now
PostMessage(MemoryHandler.GetMainWindowHandle(),
(int)KeyCodes.WMessages.WM_KEYDOWN,
(int)KeyCodes.VKeys.VK_TAB, 0);
System.Threading.Thread.Sleep(1000); // wait 1 second
//foreach process
// release keys again
PostMessage(MemoryHandler.GetMainWindowHandle(),
(int)KeyCodes.WMessages.WM_KEYUP,
(int)KeyCodes.VKeys.VK_TAB, 0);
});
Ofcourse, you have to start it.
I'm not sure exactly what you're trying to achieve, but below is the function I use to simulate text input using SendInput.
If you alter this slightly to make the final call to SendInput from a new thread, and then separate out the down and up events with a timer, does that achieve what you need?
[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 SendInput(UInt32 numberOfInputs, INPUT[] inputs, Int32 sizeOfInputStructure);
public enum InputType : uint
{
MOUSE = 0,
KEYBOARD = 1,
HARDWARE = 2,
}
struct INPUT
{
public UInt32 Type;
public MOUSEKEYBDHARDWAREINPUT Data;
}
struct KEYBDINPUT
{
public UInt16 Vk;
public UInt16 Scan;
public UInt32 Flags;
public UInt32 Time;
public IntPtr ExtraInfo;
}
public enum KeyboardFlag : uint // UInt32
{
EXTENDEDKEY = 0x0001,
KEYUP = 0x0002,
UNICODE = 0x0004,
SCANCODE = 0x0008,
}
public static void SimulateTextEntry(string text)
{
if (text.Length > UInt32.MaxValue / 2) throw new ArgumentException(string.Format("The text parameter is too long. It must be less than {0} characters.", UInt32.MaxValue / 2), "text");
var chars = UTF8Encoding.ASCII.GetBytes(text);
var len = chars.Length;
INPUT[] inputList = new INPUT[len * 2];
for (int x = 0; x < len; x++)
{
UInt16 scanCode = chars[x];
var down = new INPUT();
down.Type = (UInt32)InputType.KEYBOARD;
down.Data.Keyboard = new KEYBDINPUT();
down.Data.Keyboard.Vk = 0;
down.Data.Keyboard.Scan = scanCode;
down.Data.Keyboard.Flags = (UInt32)KeyboardFlag.UNICODE;
down.Data.Keyboard.Time = 0;
down.Data.Keyboard.ExtraInfo = IntPtr.Zero;
var up = new INPUT();
up.Type = (UInt32)InputType.KEYBOARD;
up.Data.Keyboard = new KEYBDINPUT();
up.Data.Keyboard.Vk = 0;
up.Data.Keyboard.Scan = scanCode;
up.Data.Keyboard.Flags = (UInt32)(KeyboardFlag.KEYUP | KeyboardFlag.UNICODE);
up.Data.Keyboard.Time = 0;
up.Data.Keyboard.ExtraInfo = IntPtr.Zero;
// Handle extended keys:
// If the scan code is preceded by a prefix byte that has the value 0xE0 (224),
// we need to include the KEYEVENTF_EXTENDEDKEY flag in the Flags property.
if ((scanCode & 0xFF00) == 0xE000)
{
down.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY;
up.Data.Keyboard.Flags |= (UInt32)KeyboardFlag.EXTENDEDKEY;
}
inputList[2*x] = down;
inputList[2*x + 1] = up;
}
var numberOfSuccessfulSimulatedInputs = SendInput((UInt32)len*2, inputList, Marshal.SizeOf(typeof(INPUT)));
}
Not sure what you're doing with the PostMessage but modifying some code from here:
SendKey.Send() Not working
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
public static void PressKey(Keys key, bool up)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
if (up)
{
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
}
else
{
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
}
}
void TestProc()
{
PressKey(Keys.Tab, false);
Thread.Sleep(1000);
PressKey(Keys.Tab, true);
}
Maybe this could work for you. Its just a key down and then a key up with a sleep in between. You could even add to this further and pass a time value for how long you want the key to stay down.
Goal: write a C# app that runs in the background, listens for the key combination Win-V, and when that occurs, pastes the clipboard contents into the current active window (some arbitrary app). Essentially I'm trying to mimic PureText, but I'm not bothering to convert the text to plain text first.
Problem: pasting into the currently active windows is not working.
Details: To listen in the background for key presses I'm using the globalKeyboardHook class from A Simple C# Global Low Level Keyboard Hook. I'm able to catch Win-V events, but I'm not able to send the paste command properly. I can send the paste by using the functions SendKeys.Send or keybd_event. However, they send another "V" press down the pipeline which gets caught by the gkh_KeyDown event and causes multiple paste events to fire.
I'm expecting that I need to use SendMessage or PostMessage, but all my attempts to do that have failed so far. Below is the full code with the last function, SendCtrlV, being the one of interest. The comments explain everything I've tried so far. Can you see what I'm missing?
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Utilities;
namespace KeyHookTest
{
public partial class Form1 : Form
{
private bool LWin_down;
private bool V_down;
globalKeyboardHook gkh = new globalKeyboardHook();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
gkh.HookedKeys.Add(Keys.V);
gkh.HookedKeys.Add(Keys.LWin);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
}
void gkh_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.LWin)
LWin_down = false;
else
V_down = false;
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.LWin)
LWin_down = true;
else
V_down = true;
if (LWin_down && V_down)
{
LogDebug("Enter Win+V");
try
{
SendCtrlV();
}
catch { }
}
}
private void SendCtrlV()
{
uint KEYEVENTF_KEYUP = 2;
int KEYDOWN = 0x0100;
int KEYUP = 0x0101;
byte KEY_LCONTROL1 = 0x11;
IntPtr KEY_LCONTROL2 = new IntPtr(0x11);
byte KEY_V1 = 0x56;
IntPtr KEY_V2 = new IntPtr(0x56);
int WM_PASTE1 = 0x302;
uint WM_PASTE2 = 0x302;
IntPtr hWnd = GetForegroundWindow();
// Works, but causes multiple gkh_KeyDown to fire so it's slow and buggy
/*keybd_event(KEY_LCONTROL1, 0, 0, 0);
keybd_event(KEY_V1, 0, 0, 0);
keybd_event(KEY_V1, 0, KEYEVENTF_KEYUP, 0);
keybd_event(KEY_LCONTROL1, 0, KEYEVENTF_KEYUP, 0);*/
// Works, but causes multiple gkh_KeyDown to fire so it's slow and buggy
//SendKeys.Send("^v");
// Doesn't work, causes UAC prompt
//SendKeys.Send("{^}v");
// Doesn't work, nothing gets pasted to the foregroundwindow
//SendMessage(hWnd, WM_PASTE1, 0, 0);
// Doesn't work, nothing gets pasted to the foregroundwindow
//PostMessage(hWnd, WM_PASTE2, IntPtr.Zero, IntPtr.Zero);
// Doesn't work, nothing gets pasted to the foregroundwindow
/*SendMessage(hWnd, KEYDOWN, KEY_LCONTROL1, 0);
SendMessage(hWnd, KEYDOWN, KEY_V1, 0);
SendMessage(hWnd, KEYUP, KEY_V1, 0);
SendMessage(hWnd, KEYUP, KEY_LCONTROL1, 0);*/
// Doesn't work, nothing gets pasted to the foregroundwindow
/*PostMessage(hWnd, 0x0100, KEY_LCONTROL2, IntPtr.Zero);
PostMessage(hWnd, 0x0100, KEY_V2, IntPtr.Zero);
PostMessage(hWnd, 0x0101, KEY_V2, IntPtr.Zero);
PostMessage(hWnd, 0x0101, KEY_LCONTROL2, IntPtr.Zero);*/
}
private void LogDebug(string msg)
{
string logpath = Environment.GetEnvironmentVariable("USERPROFILE") + #"\Desktop\KeyHookTest.txt";
File.AppendAllText(logpath, DateTime.Now.ToString("HH:mm:ss:fff") + ": " + msg + "\r\n");
}
}
}
These additional links helped lead me to the answer:
How to get active child window
How can I find the active child window?
Here's what's working for me:
private void SendCtrlV()
{
IntPtr hWnd = GetFocusedHandle();
PostMessage(hWnd, WM_PASTE, IntPtr.Zero, IntPtr.Zero);
}
static IntPtr GetFocusedHandle()
{
var info = new GuiThreadInfo();
info.cbSize = Marshal.SizeOf(info);
if (!GetGUIThreadInfo(0, ref info))
throw new Win32Exception();
return info.hwndFocus;
}
It works, but you must use the TextBox's native window handle if you want it to be effective
I'm using WPF and I imported the System.Windows.Form reference. Here's my code:
Process[] process = Process.GetProcessesByName("wmplayer");
SetForegroundWindow(process[0].MainWindowHandle);
Thread.Sleep(200);
System.Windows.Forms.SendKeys.Send("^p");
The Windows Media Player do Focus, but no keystroke is Received. Why?
You can use WinAPI instead of SendKeys:
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
public static void PressKey(Keys key, bool up) {
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
if (up) {
keybd_event((byte) key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr) 0);
}
else {
keybd_event((byte) key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr) 0);
}
}
void TestProc() {
PressKey(Keys.ControlKey, false);
PressKey(Keys.P, false);
PressKey(Keys.P, true);
PressKey(Keys.ControlKey, true);
}
In WPF applications you have to use SendKeys.SendWait() (English Documentation) instead.
Just doublechecked it, while Send() is working for WinForms application, WPF throws an InvalidOperationException although both target .net 4.0.
Check above link for more information.