开发者

Apply shortcut keys to Silverlight page

In application requirement is as follows.. In the Silverlight child page(Usercontrol, when you click menu item )open page it requires to fill some data then for saving we have Save button for cancel it we have Cancel button. Here i am looking开发者_Python百科 for Saving data i need to use ShortCut Keys(Ctrl + S). If i write the following code in KeyDown event it is not Functioning well, because Generally we punch the 'Ctrl' key in Presssed mode and then we punch the "S" key here if i punch 'Ctrl' key is not released then it is not working.. Otherwise ie. if punch "Ctrl" key then release it then punch "S" it is working fine..

//Code //int count=0;--Global--- protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e);

        if (e.Key == Key.Ctrl)
        {
            count = 1;

        }
        string str = e.Key.ToString();
        if (count >0 && str == "S")
        {
            //MessageBox.Show("Saved");
            //Saving the data  
            count = 0;
        }

    }

I am looking for it works on

With "Ctrl" key is in pressed mode

please look into this...

Thanks


Use Keyboard.Modifiers to check Ctrl button status. Rough example:

    if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Ctrl)
    {
        //MessageBox.Show("Saved");
        //Saving the data  
    };


The following code appears to work ok, but it seems the event will not get fired at all unless there are input controls on the page, such as text boxes etc.

public partial class MainPage : UserControl
{
    private bool _CtrlPressed;

    public MainPage()
    {
        InitializeComponent();

        LayoutRoot.KeyDown += new KeyEventHandler(LayoutRoot_KeyDown);
        LayoutRoot.KeyUp += new KeyEventHandler(LayoutRoot_KeyUp);
    }

    void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Ctrl)
            _CtrlPressed = true;
    }

    void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Ctrl)
            _CtrlPressed = false;

        if (e.Key == Key.S && _CtrlPressed)
        {
            Debug.WriteLine("Ctrl + S was pressed");
            //Execute save operation
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜