Custom Keyboard Provider

12 posts, 0 answers
  1. Koren
    Koren avatar
    70 posts
    Member since:
    Oct 2010

    Posted 16 May 2011 Link to this post

    Our users need to be able to enter data in the grids using the number pad which means that I need the Enter key to work like the Tab key for adding data.  I got this close to working using a CustomKeyboardCommandProvider where I assign the Enter key to the Tab key commands.  However, I would actually prefer to only have it work this way if I am inserting a record and let it work as normal if the user is just trying to go back and update a field.  I use the AddingNewDataItem to add a new object when inserting a record.  Is there any way in the CustomKeyboardCommandProvider to know when I am inserting vs updating a record? Or is there a better way to get this to work?

    Also, if they are in insert mode, I would like to be able to take them to a new record after the current record is committed. Can I add a BeginInsert command to the list of commands after the Commit to get this to work?  I noticed that the tab key list does not include a CommitEdit command when it wraps from one record to another so I am not sure where to add this BeginInsert?

    thanks!! 

  2. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 18 May 2011 Link to this post

    Hi Koren,

    You may handle the AddingNewDataItem and RowEditEnded events like follows:

    void playersGrid_AddingNewDataItem(object sender, Telerik.Windows.Controls.GridView.GridViewAddingNewEventArgs e)
            {
                e.NewObject = new Player();
                this.playersGrid.KeyboardCommandProvider = new CustomKeyboardCommandProvider(this.playersGrid);
            }
     
            void playersGrid_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
            {
                this.playersGrid.KeyboardCommandProvider = new DefaultKeyboardCommandProvider(this.playersGrid);
            }


    The CustomKeyboardCommandProvider may be defined as below:
    public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
        {
            private GridViewDataControl parentGrid;
     
            public CustomKeyboardCommandProvider(GridViewDataControl grid)
                : base(grid)
            {
                this.parentGrid = grid;
            }
     
            public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
            {
                List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
                if (key == Key.Enter)
                {
                    commandsToExecute.Clear();
                    commandsToExecute.Add(RadGridViewCommands.MoveNext);
                    commandsToExecute.Add(RadGridViewCommands.BeginEdit);
                }
     
                return commandsToExecute;
            }
        }
     
    Let me know whether this corresponds to your requirements.

    Best wishes,
    Maya
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Koren
    Koren avatar
    70 posts
    Member since:
    Oct 2010

    Posted 18 May 2011 Link to this post

    Brilliant.  Worked just as I needed.

    thanks!
  4. Koren
    Koren avatar
    70 posts
    Member since:
    Oct 2010

    Posted 27 Jan 2015 in reply to Koren Link to this post

    I just upgraded to Q3 2014 and this keyboard handler stopped working.  When I get to the end of the row where it should commit and begin another row, it just commits and goes out of edit mode.  Was there a change in 2014 that I should be aware of?
  5. Koren
    Koren avatar
    70 posts
    Member since:
    Oct 2010

    Posted 27 Jan 2015 Link to this post

    Sorry - I just realized that my logic is a little different than what was posted...

    public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
            {
                List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();
     
                bool _shiftIsPressed = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
                int _lastColumnIndex = 0;
     
                if (key == Key.Tab || key == Key.Enter)
                {
                    bool _onLastColumn = false;
     
                    if (!String.IsNullOrEmpty(_lastColumnName))
                    {
                        if (this.parentGrid.CurrentColumn.UniqueName.ToLower() == _lastColumnName.ToLower())
                            _onLastColumn = true;
                    }
                    else
                    {
                        _lastColumnIndex = FindLastEditableColumn();
     
                        var lastColumn = this.parentGrid.Columns[_lastColumnIndex] as Telerik.Windows.Controls.GridViewColumn;
                        if (this.parentGrid.CurrentColumn == lastColumn)
                            _onLastColumn = true;
                    }
     
                    if (_onLastColumn)
                    {
                        if (_shiftIsPressed)
                        {
                            commandsToExecute.Clear();
                            commandsToExecute = base.ProvideCommandsForKey(Key.Tab).ToList();
                        }
                        else
                        {
                            commandsToExecute.Clear();                       
                            commandsToExecute.Add(RadGridViewCommands.CommitEdit);
                            commandsToExecute.Add(RadGridViewCommands.BeginInsert);
                        }
                    }
                    else
                        if (key == Key.Enter)
                        {
                            commandsToExecute.Clear();
                            commandsToExecute = base.ProvideCommandsForKey(Key.Tab).ToList();
                        }
                }           
     
                _lastKey = key;
                return commandsToExecute;
                 
            }
  6. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 29 Jan 2015 Link to this post

    Hi Koren,

    I might be missing something here, but did you manage to resolve the issue that you had or do you need an assistance ? 

    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  7. Koren
    Koren avatar
    70 posts
    Member since:
    Oct 2010

    Posted 19 Feb 2015 in reply to Maya Link to this post

    No I did not fix this issue.  It commits the edit but does not BeginInsert at the end of the row like it did before for either the Tab or the Enter key.  Are these not the correct commands any more?
  8. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 20 Feb 2015 Link to this post

    Hello Koren,

    It seems that there is a delay in committing the changes for the newly added item. What you can try is:
    commandsToExecute.Clear();
                parentGrid.Items.CommitNew();
                commandsToExecute.Add(RadGridViewCommands.CommitEdit);
                commandsToExecute.Add(RadGridViewCommands.BeginInsert);




    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  9. Koren
    Koren avatar
    70 posts
    Member since:
    Oct 2010

    Posted 20 Feb 2015 in reply to Maya Link to this post

    That doesn't work.  It just leaves me with what looks like an empty record at the bottom and doesn't go into insert mode still.  If I go away from that tab and come back, it looks like my record was actually committed but it doesn't look that way during entry.

    My keyboard command provider is set up to keep the grid in insert mode for either the tab or enter key.  So when I hit the last column it should commit the edit and then begin insert.  After the edit is committed, I can see that BeginInsert is in the PendingCommands but it never happens.
  10. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 23 Feb 2015 Link to this post

    Hi Koren,

    I tested the code and it seems to work correctly on my side. Could you check out the project attached ? Do you get the expected behavior on it ?

    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  11. Koren
    Koren avatar
    70 posts
    Member since:
    Oct 2010

    Posted 23 Feb 2015 in reply to Maya Link to this post

    I will open a ticket with the updated project including my results.
  12. Maya
    Admin
    Maya avatar
    4062 posts

    Posted 24 Feb 2015 Link to this post

    Hello Koren,

    The issue appears as NewRowPosition is set. I resolved the issue and it will be available in one of our next internal builds. 
    Thanks for sending the project and the cooperation. I updated your Telerik points.

    Regards,
    Maya
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top