Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're sending the keys while the <kbd>Shift</kbd> key is still down, which is causing them to be uppercased.<br> You need to figure out a way to cancel the <kbd>Shift</kbd> key press along with the <kbd>K</kbd> key press.</p> <p>The global hook sample that you're using is a little bare; it should <em>also</em> be reporting which modifier keys are held down. Unfortunately, it looks like that functionality hasn't been implemented.</p> <p>Why do you need to use a keyboard hook in the first place? Do you really need to handle key events that occur when your form <em>doesn't</em> have the focus? And if so, why in the world would you use <code>SendKey</code>? How do you know what the currently active application is going to do with the key presses you send?</p> <p>This looks like something that would be much better handled in an override of your form's <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.processcmdkey.aspx" rel="nofollow noreferrer"><code>ProcessCmdKey</code> method</a>, instead. For example:</p> <pre><code> protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.K | Keys.Shift)) { SendKeys.Send("b"); return true; // indicate that you handled the key } else if (keyData == Keys.K) { SendKeys.Send("a"); return true; // indicate that you handled the key } // call the base class to handle the key return base.ProcessCmdKey(ref msg, keyData); } </code></pre> <hr> <p><strong>EDIT:</strong> Your comment suggests that you do in fact need to handle key events that occur when you form does <em>not</em> have the focus. Assuming you need to handle more than simply the <kbd>K</kbd> key, you will need to do this using a global hook.</p> <p>As I mentioned before, the problem is that the user is still holding down the <kbd>Shift</kbd> key while you're sending the <kbd>B</kbd> key using <code>SendInput</code>, which is causing it to register as a capital letter B instead of a lowercase letter. So then the solution is obvious: you need to figure out a way to <em>cancel</em> that <kbd>Shift</kbd> key press so that it is not processed by the operating system. Of course, if you eat the key event, you also need to figure out a way of <em>tracking</em> it so that your application still knows when it was pressed and can act accordingly.</p> <p>A quick search reveals <a href="https://stackoverflow.com/questions/2914989/how-can-i-deal-with-depressed-windows-logo-key-when-using-sendinput">a similar question</a> has already been asked and answered about the <kbd><img src="https://i.stack.imgur.com/FdKGs.png" alt="Windows logo"></kbd> key.</p> <p>In particular, you need to write the code that handles the <code>KeyDown</code> event raised by your global hook like this (at least, this code works with the global hook class that I wrote; it should work with yours, too, but I haven't actually tested it):</p> <pre><code>// Private flag to hold the state of the Shift key even though we eat it private bool _shiftPressed = false; private void gkh_KeyDown(object sender, KeyEventArgs e) { // See if the user has pressed the Shift key // (the global hook detects individual keys, so we need to check both) if ((e.KeyCode == Keys.LShiftKey) || (e.KeyCode == Keys.RShiftKey)) { // Set the flag _shiftPressed = true; // Eat this key event // (to prevent it from being processed by the OS) e.Handled = true; } // See if the user has pressed the K key if (e.KeyCode == Keys.K) { // See if they pressed the Shift key by checking our flag if (_shiftPressed) { // Clear the flag _shiftPressed = false; // Send a lowercase letter B SendKeys.Send("b"); } else { // Shift was not pressed, so send a lowercase letter A SendKeys.Send("a"); } // Eat this key event e.Handled = true; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload