How do i Turn OFF the Caps lock key in textbox. I am using WPF forms.
When textbox is focused I want to turn off caps lock.
Thanks
Its easy , Firstly add namespace
using System.Runtime.InteropServices;
then declare this in the class
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
Finally , at textBox_Enter event add this code
private void textBox1_Enter(object sender, EventArgs e)
{
if (Control.IsKeyLocked(Keys.CapsLock)) // Checks Capslock is on
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr)0);
}
}
this code will turn off the Capslock .. I have used it at the enter event you can add it according to your requirement!
Checkout this link here
Use this code for WPF froms.
private void txt_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled) // Checks Capslock is on
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr)0);
}
}
Related
I´m trying to simulate a mouse click when certain key is pressed.
I tried this:
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x08;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Insert:
Point pt = Cursor.Position;
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_RIGHTDOWN, X, Y, 0, 0);
break;
}
It doesn't seems to be working, and I couldn't find any other solution.
First make sure the Form KeyPreview property is set to True
To do the click simulation you need to call MOUSEEVENTF_RIGHTDOWN and MOUSEEVENTF_RIGHTUP like so. (also notice i use uint along the way)
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Insert:
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, (uint)X, (uint)Y, 0, 0);
break;
}
}
I'm trying to use the Sendkeys to simulate the Windows start key, but none of the options I tried work, does anybody know how can it be done?
CODE
[System.Runtime.InteropServices.DllImport("user32.dll")]
private Thread thrTyping;
private void startThread()
{
ThreadStart ts = new ThreadStart(sendKeys);
thrTyping = new Thread(ts);
thrTyping.Start();
}
private void sendKeys()
{
// TEST 1
Thread.Sleep(5000);
SendKeys.SendWait("(^)"+"{ESC}");
// TEST 2
Thread.Sleep(5000);
SendKeys.SendWait("{LWin}");
}
Use keybd_event instead:
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
private static void PressKey(byte keyCode)
{
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
List of KeyCodes (The one you are looking for is 0x5B - left win key)
I've written a WPF touchscreen application. Within this application I've written a user control which is a touch screen keyboard.
I have all the keys working apart from the backspace. Obviously all the keys are buttons and on the backspace button click I'd like to send the back space key to the focused textbox.
I've tried the following but this just inserts a square character in my textbox:
private void buttonBackSpace_Click(object sender, RoutedEventArgs e)
{
PresentationSource source = PresentationSource.FromDependencyObject(CurrentControl);
//CurrentControl is my Textbox
KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Key.Back)
{
RoutedEvent = UIElement.KeyDownEvent
};
System.Windows.Input.InputManager.Current.ProcessInput(ke);
}
Any ideas? I'm using Visual Studio 2012 & Windows 8.
I'm not sure if this is a typo or not, but I think your issue is that
KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Key.Back)
Should actually be:
KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Keys.Back)
I think the Keys enumeration is what you're actually looking for here.
EDIT: The above answer does not work. The below answer should, however, solve the problem.
private void buttonBackSpace_Click(object sender, RoutedEventArgs e)
{
CurrentControl.Focus();
keybd_event(BACK, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(BACK, 0, KEYEVENTF_KEYUP, 0);
}
// Virtual Keypress Function
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const int KEYEVENTF_KEYDOWN = 0x0001; //Key down flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
public const int BACK = 0x08; // Backspace keycode.
Hi i have a c# application and an embedded browser in it,its task is to find a link and then right click on it and click on properties!
mouse moves programmatically,so i need to find properties in righ click menu!
can you help me how to do this?
i tried pressing 'r' after right click but it didn't work on some computers!
so i need to do it by moving mouse!
here is my code for finding a link and right clicking:
int x = getXoffset(link);
int y = getYoffset(link);
webBrowser1.Document.Window.ScrollTo(x, y);
Linker.Win32.POINT p2 = new Linker.Win32.POINT();
webBrowser1.Focus();
p2.x = webBrowser1.Left + 10;
p2.y = webBrowser1.Top + 5;
Linker.Win32.ClientToScreen(this.Handle, ref p2);
Linker.Win32.SetCursorPos(p2.x, p2.y);
MouseOperations.GetCursorPosition();
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightUp);
Any other idea for reaching properties on right click meny is welcomed
use this code:
[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);
}
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://google.com");//Your link
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Find your link and right click(automatically by your code)
webBrowser1.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
}
void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
if (e.MouseButtonsPressed == MouseButtons.Right)
{
Thread.Sleep(1000);
PressKey(Keys.P, true);
PressKey(Keys.P, false);
}
}
I have a ListBox with SelectionMode = MultiExtended. I want the default behavior for the ListBox to be "append". In other words, the behavior you get when holding down the control key should be the default, passive functionality for the ListBox.
How would I do this? Do I need to subscribe to the "Mouse Down" and "Key Down" events manually? Is there a setting I'm missing?
Thanks.
Ugly solution but the best I could do.
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const byte KEYEVENTF_KEYUP = 0x02;
public const int VK_CONTROL = 0x11;
private void listBox1_MouseEnter(object sender, EventArgs e)
{
keybd_event(VK_CONTROL, (byte)0, 0, 0);
}
private void listBox1_MouseLeave(object sender, EventArgs e)
{
keybd_event(VK_CONTROL, (byte)0, KEYEVENTF_KEYUP, 0);
}
Use MultiSimple Mode
http://msdn.microsoft.com/en-us/library/system.windows.forms.selectionmode(v=vs.80).aspx
SelectionMode = SelectionMode.MultiSimple