JavaScript - Call Ctrl+P Keyboard shortcut in javascript

Asked By Ganesan BalaKrishnan on 14-May-09 02:38 AM

How to call Ctrl+P Keyboard shortcut in javascript

For example i have one button and iframe in the webpage and i loaded PDF file in the iframe. now if i press that button than i need to call Ctrl+P shortcut and i need to print that PDF file.

If any one know answer Please help me...


Santhosh N replied to Ganesan BalaKrishnan on 14-May-09 02:45 AM

Check this one explaining how to do this in javascript..

http://www.openjs.com/scripts/events/keyboard_shortcuts/

Not Possible

Prakash C replied to Ganesan BalaKrishnan on 14-May-09 02:50 AM
By default it will invoke the browser print window.


Ravenet Rasaiyah replied to Ganesan BalaKrishnan on 14-May-09 02:55 AM
Hi

To add short cut keys by the java script in your button write a  function like this

Javascript:
function PrintShortCutKey()

{

//User hit Ctrl+S

if (window.event.keyCode == 80)

{

// action

}

}

Then call that function within you button click

HTML Code

<input id="Button1" type="button" onclick="javascript:PrintShortCutKey();" value="button" />

Then it will work fine.

Thank you
Break the default action of the browser thru javascript.
Prakash C replied to Ganesan BalaKrishnan on 14-May-09 03:16 AM
u can capture the keycode and break the default action of the browser thru javascript.

like this...

    <script language="javascript" type="text/javascript" >
    var isCtrl = false;     
    document.onkeyup=function()
    {
        var e = window.event;
        if(e.keyCode == 17)
        {
            isCtrl=false;
        }
    }
    
    document.onkeydown=function()
    {
        var e = window.event;
        
        if(e.keyCode == 17)
        {
            isCtrl=true;
        }
        if(e.keyCode == 80 && isCtrl == true) // Ctrl + P
        {
            alert('hi'); // write ur stuff here...
        }
    }
    </script>

Hope this helps... :-)