locked
Turn capslock on and off problem.. RRS feed

  • Question

  • Hi,

    In my WPF application I've few TextBoxes that I want to turn capslock on when they got the focus and off when they lose focus.

    I've two API's:

    private void Turn_CapsLOCK_ON()
            {
                bool isCapsLock = (((ushort)GetKeyState(0x14 /*VK_CAPITAL*/)) & 0xffff) != 0;
                if (isCapsLock == false && this.check_capsLock == true)
                {
                    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.check_capsLock = false;
                }
            }

            private void Turn_CapsLOCK_OFF()
            {
                bool isCapsLock = (((ushort)GetKeyState(0x14 /*VK_CAPITAL*/)) & 0xffff) != 0;
                if (isCapsLock == true && this.check_capsLock == true)
                {
                    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.check_capsLock = false;
                }
            }

     

    i call this API's with GotFocus and LostFocus events.

    my problem is that if I move from textbox that turned on capslock to other textbox that should turn the capslock on, the caps lock is turned off.

    when the first TextBox got the focus, it turned the capsLock, then when i moved the focus to the second TextBox, fist the Lost focus of the first textbox called (and turned off caps lock) and then the GotFocus of the second TextBox "thinks" that the caps lock is on and doesn't turn it on... (bool isCapsLock = (((ushort)GetKeyState(0x14 /*VK_CAPITAL*/)) & 0xffff) != 0;

    return True).

    how can i fix that?

     

    Thanks,

    Gidi.

    Wednesday, September 29, 2010 12:20 PM

Answers

  • Hello Guys,

     

    I use the caps lock event to switch between hebrew and english.

    in my window most of the textboxes expect an Hebrew input, and some are expecting an English input.

    if there's another elegant way to do it, i will be happy to learn about it...

     

    Thanks,

    Gidi.


    I see. If that is the case, you can simply set the InputLanguageManager.InputLanguage property on the TextBox, so when the TextBox gets focus, the IME will be automatically swtiched to the corresponding language:

    <TextBox InputLanguageManager.InputLanguage="he-il"/> <!-- If he-il is the default, you don't need to set it explicitly. -->
    <TextBox InputLanguageManager.InputLanguage="en-us" InputLanguageManager.RestoreInputLanguage="True"/>
    <TextBox/>
    <TextBox InputLanguageManager.InputLanguage="en-us" InputLanguageManager.RestoreInputLanguage="True"/>
    

    Just one thing: you need to make sure the corresponding IME has been installed.

    Will that solve the problem?

    Thanks,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    • Marked as answer by gidi Sunday, October 10, 2010 9:07 PM
    Thursday, October 7, 2010 5:27 AM

All replies

  • Sorry for asking, but... is this really necessary? If your goal is to make sure that the content of the TextBox is always uppercase there are easier (and safer) alternatives.

    Care to explain what your requirements are? Maybe we can find a better solution.

    Thanks
    --mc

    Thursday, September 30, 2010 6:06 PM
  • I totally agree with Mario. That said, you've got 

     

     this.check_capsLock = false;

     

    in both methods... should one be setting it to true?

     

    If that doesn't fix it, perhaps ask the TextBox getting focus to turn it off then on when it does get focus.

     

    Warm regards,

    Matt

    Friday, October 1, 2010 1:53 AM
  • Hello Gidi,

    The problem is NOT the sequence of the LostFocus / GotFocus event, rather it is because after you send the keys, certain messages are not processed yet, causing the misjudgement of the Caps Lock toggle state.

    So if we simply focusing on this specific problem, we can let the messages be processed after the keys are sent. In Windows Forms, we can call Application.DoEvent to achieve this (call Application.DoEvent after keybd_event in LostFocus). However, in WPF, there is no elegant way. You can create a nested dispatcher frame to process the messages, but this is not recommended. You can search 'WPF DoEvents equivalent' and will find more information.

    Looking at the bigger picture, I have the same question as Mario and Matt do. What's the purpose of this feature? Can you elaborate the scenario so we can see if there are any better ways to do that?

    Thanks!
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    Friday, October 1, 2010 8:27 AM
  • Hello Guys,

     

    I use the caps lock event to switch between hebrew and english.

    in my window most of the textboxes expect an Hebrew input, and some are expecting an English input.

    if there's another elegant way to do it, i will be happy to learn about it...

     

    Thanks,

    Gidi.

    Sunday, October 3, 2010 7:23 PM
  • Hello Guys,

     

    I use the caps lock event to switch between hebrew and english.

    in my window most of the textboxes expect an Hebrew input, and some are expecting an English input.

    if there's another elegant way to do it, i will be happy to learn about it...

     

    Thanks,

    Gidi.


    I see. If that is the case, you can simply set the InputLanguageManager.InputLanguage property on the TextBox, so when the TextBox gets focus, the IME will be automatically swtiched to the corresponding language:

    <TextBox InputLanguageManager.InputLanguage="he-il"/> <!-- If he-il is the default, you don't need to set it explicitly. -->
    <TextBox InputLanguageManager.InputLanguage="en-us" InputLanguageManager.RestoreInputLanguage="True"/>
    <TextBox/>
    <TextBox InputLanguageManager.InputLanguage="en-us" InputLanguageManager.RestoreInputLanguage="True"/>
    

    Just one thing: you need to make sure the corresponding IME has been installed.

    Will that solve the problem?

    Thanks,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The All-In-One Code Framework Project
    My Blog (in Simplified Chinese)
    • Marked as answer by gidi Sunday, October 10, 2010 9:07 PM
    Thursday, October 7, 2010 5:27 AM
  • Thanks!

     

    Now, how can I set that the text will always be Upper Case?

    Sunday, October 10, 2010 9:08 PM