Share via


How to send Left/Right shift key

Question

Saturday, November 15, 2008 11:14 AM | 1 vote

Hi
I want to send left or right shift key, but System.Windows.Forms.SendKeys.Send method does not support it. How can I do?

thanks in advance

All replies (26)

Wednesday, November 19, 2008 9:28 AM âś…Answered | 1 vote

Hi Mehdi Rafeie,

Please try the following code.

1 using System.Runtime.InteropServices;  
2 using System.Diagnostics;  
3  
4 private const int WM_KEYDOWN = 0x100;  
5  
6 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]  
7 static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);  
8  
9 private void button1_Click(object sender, EventArgs e)  
10 {  
11     Keys key = Keys.RShiftKey;//Right shift key  
12     SendMessage(Process.GetCurrentProcess().MainWindowHandle, WM_KEYDOWN, (int)key, 1);  
13 }  
14  
15 protected override void DefWndProc(ref Message m)  
16 {  
17     if (m.Msg == WM_KEYDOWN)  
18         MessageBox.Show(m.WParam.ToString());  
19  
20     base.DefWndProc(ref m);  
21 }  
22  

From the above code, you can send and get "Right shift key" value.

But I don't think you only want to send left or right shift key, you may want to process some other things when you send the key. So please let me know your purpose when you still have problem.

Best regards,
Guo Surfer


Saturday, November 15, 2008 2:57 PM

Not a big fan of SendKeys, but it seems like it does.

SendKeys.Send("{LEFT}");  
SendKeys.Send("{RIGHT}"); 

These are listed in the documentation:

http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx


Saturday, November 15, 2008 6:59 PM

SendKeys.Send("+a");   //result A  plus for shift  


Let's Program.. , Yam Sapkota


Monday, November 17, 2008 6:13 AM

BinaryCoder said:

Not a big fan of SendKeys, but it seems like it does.

SendKeys.Send("{LEFT}");  
SendKeys.Send("{RIGHT}"); 

These are listed in the documentation:

http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx

As we know {LEFT} code denotes left arrow key. I'm looking for a way to send Left Shift Key or Right Shift Key.
I recommend you to read this document first, then reply!!!
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send.aspx


Monday, November 17, 2008 6:17 AM

Yam Sapkota said:

SendKeys.Send("+a");   //result A  plus for shift  


Let's Program.. , Yam Sapkota

thanks for your reply, but...

SendKeys.Send("+") means holding Shift key down, but Left shift key or right one?


Thursday, November 20, 2008 7:16 AM

Hi dear Guo,

Assume there is a RichTextBox control on a form which has input focus.
If RShift / LShift + Ctrl keys are pressed simultaneously, the direction of current paragraph of the RichTextBox will be changed to Right-To-Left / Left-To-Right. Please note that this event differs from the change of text alignment to right or left. (that has Ctrl+R / Ctrl+L shortcut key)
Hence, I decide to write a code to send the above keys to a RichTextBox to simulate the event of paragraph direction change, Just when a specific button is pressed.
Please guide me!

with many thanks


Thursday, November 20, 2008 9:25 AM

With you last clarification, your question has been answered:  prepend +. 


Thursday, November 20, 2008 10:43 AM

I used Guo's code. It works correctly and sends any specified key exactly, but my RichTextBox text direction does NOT change! This is my problem.


Thursday, November 20, 2008 12:11 PM | 1 vote

Hi Mehdi Rafeie,

Since you can send left/right shift key as you mentioned above, then the next work is to process this key message.

You can generate a new class inherited from RichTextBox and only override the message processing function WndProc(). Change the RightToLeft property when receiving the message.

    public class MyRichTextBox : RichTextBox  
    {  
        private const int WM_KEYDOWN = 0x100;  
 
        protected override void WndProc(ref Message m)  
        {  
            if (m.Msg == WM_KEYDOWN)  
            {  
                if (m.WParam == (IntPtr)(Keys.LShiftKey))  
                {  
                    this.RightToLeft = RightToLeft.Yes;  
                    this.Refresh();  
                }  
                else if (m.WParam == (IntPtr)(Keys.RShiftKey))  
                {  
                    this.RightToLeft = RightToLeft.No;  
                    this.Refresh();  
                }  
            }  
            base.WndProc(ref m);  
        }  
    } 

When you press left/right shift on your keyboard, it only send shift message, not sending left/right shift message, so it does not affect the result.

Please have a try.

Best regards,
Guo Surfer

 


Thursday, November 20, 2008 1:04 PM

Hi Guo Surfer,
The RichTextBox control loses formatting when you change the RightToLeft property at run time! So I can't use this property. On the other hand, if Left/Right shift + ctrl keys are pressed, no problem will be raised!! Therefore I tried to send these shortcut keys to the RichTextBox.

Thanks a lot
Mehdi


Friday, November 21, 2008 6:29 AM

 Dear Guo Surfer,
I heed your help.
Although my initial question is replied by you (I marked it as answer), but I couldn't achieve the main purpose. As we know left/right shift + Ctrl is an intrinsic shortcut key for the RichTextBox control. With the aid of your code, I can send these keys to the RichTextBox as follows:

Keys key = Keys.RShiftKey | Keys.Control;
SendMessage(MyRichTextBox.Handle, WM_KEYDOWN, (int)key, 1);

I expected 'MyRichTextBox' text direction to change, but it didn't affect at all. What's the reason for?

Youstruly,
Mehdi Rafeie


Sunday, November 23, 2008 6:25 AM

Is there somebody who can help me???


Sunday, November 23, 2008 7:16 AM

Your version of the RTB must be different than the English version, since, with the RTB having the focus on my system, pressing any combination of the Shift keys and the Control keys has no effect.


Sunday, November 23, 2008 8:43 AM

Hence, What should I do?
(Would you mind talking more about that?)

thanks in advance,

Mehdi Rafeie


Sunday, November 23, 2008 9:02 AM

Can you describe exactly how you get the result you describe using the keyboard?  I assume when you press the RightShift and Control keys, the RightToLeft property changes, but you don't loose the formatting.  Is that correct?  Now you want to duplicate this behaviour programmatically.  Right?


Sunday, November 23, 2008 10:36 AM

JohnWein said:

Can you describe exactly how you get the result you describe using the keyboard?  I assume when you press the RightShift and Control keys, the RightToLeft property changes, but you don't loose the formatting.  Is that correct?

Yes, that's right.

Please refer to VS 2008-Help URL:
    ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/enu_kbvbnetkb/vbnetkb/814310.htm

The reason I can't accept the solution that has been introduced in the above URL is because it's not really perfect and I encounter some problems with. For example, if I change RightToLeft property of the last paragraph and continue typing, I'll not be able to change the RightToLeft property of that paragraph again. (when it's not the last one.) And there are some other reasons too... Therefore the unique solution of this problem seems to be to press left/right shift and control keys. When I press these keys simultaneously, the RightToLeft property of the RTB changes successfully! But nothing occurs when I send these keys to the RTB!!!


Sunday, November 23, 2008 12:06 PM

Why isn't the workaround in your link accecptable?  What is the purpose of Guo Surfer's last post?  I don't understand why, if you press the keys on the keyboard, you get the behaviour you desire, but if you send the keys to the RTB in a message you don't get the desired behaviour.  What culture version of VS are you using?


Sunday, November 23, 2008 12:39 PM

Where can I find culture version of my VS?
My VS version is: 9.0.21022.8 RTM
I've installed files for complex script and right-to-left languages from Control Panel/Regional and Language Options and I've selected Farsi as a language to match the language version of the non-Unicode programs I wanna use. Is this helpful?


Sunday, November 23, 2008 1:11 PM

Supposedly this was fixed in VS2005 and later.  I guess it wasn't.  Send "+R" or "+L".  Save and restore the paragraph in the RTB's RightToLeft property.


Monday, November 24, 2008 5:48 AM

JohnWein said:

Supposedly this was fixed in VS2005 and later.  I guess it wasn't.  Send "+R" or "+L".  Save and restore the paragraph in the RTB's RightToLeft property.

"+R" -> "^R"
"+L" -> "^L"
Ok?

I said:

... If RShift / LShift + Ctrl keys are pressed simultaneously, the direction of current paragraph of the RichTextBox will be changed to Right-To-Left / Left-To-Right. Please note that this event differs from the change of text alignment to right or left. (that has Ctrl+R / Ctrl+L shortcut key) ...


Monday, November 24, 2008 6:40 AM

  • If you press End / Home key while text alignment is setted to right(left) and RightToLeft property to no, the caret will go to Right / Left hand side of the sentence.
  • If you press End / Home key while text alignment is setted to right(left) and RightToLeft property to yes, the caret will go to Right / Left hand side of the sentence.

Monday, November 24, 2008 8:17 AM

I can't find any difference between Control + R or L and Control + RShift Or LShift.  Can you post a sample that shows the difference?  Why doesn't saving and restoring the paragraph work?


Monday, November 24, 2008 10:09 AM

Mehdi Rafeie said:

  • If you press End / Home key while text alignment is setted to right(left) and RightToLeft property to no, the caret will go to Right / Left hand side of the sentence.
  • If you press End / Home key while text alignment is setted to right(left) and RightToLeft property to yes, the caret will go to Right / Left hand side of the sentence.

As was mentioned previously, text alignment is different from text direction. This is a salient difference for anybody using righ-to-left languages. Are you using these languages on your PC?


Monday, November 24, 2008 11:24 AM | 1 vote

"Therefore the unique solution of this problem seems to be to press left/right shift and control keys. When I press these keys simultaneously, the RightToLeft property of the RTB changes successfully!"

On a US keyboard, no combination of keys will change the RightToLeft property.  If you post your keyboard culture, perhaps someone with the same keyboard can assist you.


Monday, November 24, 2008 11:48 AM

Dear John Wein
I wrote an application to test RTB, but unfortunately I can't attach it here!
Would you mind sending me your email address.

[email protected]


Monday, November 24, 2008 12:32 PM

JohnWein said: 
no combination of keys will change the RightToLeft property

Oh I'm so sorry! I meant the behavior of the RTB is just like when it's RightToLeft property changes. Although if you check this property after pressing these keys, it won't change at all.