How to prevent default functionality of CTRL+X in Spread V11

Posted by: viswanath.malepati on 13 February 2018, 6:20 am EST

  • Posted 13 February 2018, 6:20 am EST

    Hi,

    We have a scenario where based on a condition we don’t allow user to cut the cells even if he presses CTRL+x.

    in spread v9, we achieved it by writing an addKeyMap for CTRL+x key and return false based on a condition.

    in spread v11, we did same thing but with commandManager’s register and setShortcutKey, as you guys suggested. But here although we return false, default functionality is not prevented. Looks like this is more like a event handler kind of thingy in v11.

    So, can someone suggest what is better way to restrict user to cut cells based on a condition is Spread V11?

    please find the working code in spread9.html and code that is not working in spread11.html, that are attached to this post.

  • Posted 13 February 2018, 6:21 am EST

  • Posted 15 February 2018, 3:19 am EST

    Hello,

    You can check for a condition and then cancel Ctrl+x keys. Similar to the code below:

    
     if(true){
    			spread.commandManager().setShortcutKey(undefined, GC.Spread.Commands.Key.x, true, false, false, false);
                }
    
    

    To cancel the function you simply use the ‘return;’ statement.

    Thanks,

    Deepak Sharma

  • Posted 15 February 2018, 10:57 am EST

    Hi Deepak,

    I actually tried using ‘return;’ in the registered function. You can actually see that in the spredv11.html file line number 22. But it is still cutting the cells.

    And when it comes to the above solution that you suggested

    
    if(true){
    			spread.commandManager().setShortcutKey(undefined, GC.Spread.Commands.Key.x, true, false, false, false);
                }
    
    

    It is not not apt to our requirement. Because in the above case we are setting a short cut key based on condition, but setting a short cut key is not triggering. So, what I feel is condition should be inside the handler but the above code is something different. Please correct me if I am wrong. And it would be great if you can share a working sample which is inlines as below:

    Lemme explain a sample use case:

    I want to restrict CUT of cells, if atleast one of the cell in the range selected is locked. Now, lets say user selected a range of cells and one cell in that range is locked. Now even when he presses CTRL+X the range should not be cut.

    For this, in V9 we found a way by using addKeyMap and based on condition we return false. When we tried to do same thing in v11 it is not working.

    Let me know if you need more clarity.

    Thanks,

    Viswanath

  • Posted 19 February 2018, 7:24 am EST

    Hi Viswanath,

    You can use the code as follows to handle Ctrl +X:

    
    <div id="ss"  onkeydown="keyDownFunction(event)"></div>
        <script>
            var spread, sheet;
            window.onload = function () {
                 spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {});
                sheet = spread.getActiveSheet();
    		
            }
            function keyDownFunction(event)
            {
    
              if(event.ctrlKey && event.keyCode == 88){
                console.log("Ctrl + X!");
             event.returnValue = false;
          }
            }
    
        </script>
    </body>
    
    
    

    Thanks,

    Deepak Sharma

  • Posted 20 February 2018, 7:59 am EST

    It works. We actually tried this out, but is there a way to do it based on spread APIs?

  • Posted 21 February 2018, 10:41 am EST

    Hello,

    Sorry, I could not find any other to to achieve this.

    Thanks,

    Deepak Sharma

  • Posted 22 February 2018, 6:19 am EST

    Hi , How to prevent default functionality of backspace key in spread js 11

  • Posted 22 February 2018, 8:13 am EST

    Hi Shalini,

    You can use the code similar to above given by me:

    <div id="ss"  onkeydown="keyDownFunction(event)"></div>
        <script>
            var spread, sheet;
            window.onload = function () {
                 spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {});
                sheet = spread.getActiveSheet();
    		
            }
            function keyDownFunction(event)
            {
    
              if(event.keyCode == 8){
    alert("Backspace pressed");
             event.returnValue = false;
          }
            }
    
        </script>
    </body>
    

    Thanks,

    Deepak Sharma

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels