Can a script determine how it was launched?

Richard HaseltineRichard Haseltine Posts: 100,905

Is it possible for a script to determine whether it was launched via a keyboard shortcut (such as ctrl-something)) or via some other route (menu, toolbar or content pane)? I was wondering if it would be possible to have rthe script ignore modifier keys if it was launched via a shortcut, rather than have control-freak type behaviour fire up when using ctrl/cmd in a combination.

Comments

  • rbtwhizrbtwhiz Posts: 2,250
    edited December 1969

    DzApp::modifierKeyState() provides the pressed state of modifier keys that can be checked against the values from Qt::KeyboardModifier:

    var g_bShiftPressed = App.modifierKeyState() & 0x02000000;
    var g_bControlPressed = App.modifierKeyState() & 0x04000000;
    var g_bAltPressed = App.modifierKeyState() & 0x08000000;

    DzApp::isKeyDown() provides the pressed state of keys using the numeric values from Qt::Key:

    var g_bCapsLock = App.isKeyDown( 0x01000024 );

    DzApp::isKeySequenceDown() provides the pressed state of keys using a "PortableText" string:

    var g_bShiftAPressed = App.isKeySequenceDown( "Shift+A" );

    You could also make use of the Sub Script sample to execute a common script in various ways; passing in arguments that control the mode of operation. One script could check modifier state and pass that along, while another doesn't.

    -Rob

  • Richard HaseltineRichard Haseltine Posts: 100,905
    edited December 1969

    Right, but if a user assigns a keyboard shortcut - say ctrl-alt-shift-s - to launch Script then some modifiers will be held down (or not) as part of the shortcut, not necessarily with he intent of triggering the special behaviour. What I was wondering was if there's a way to distinguish the launch method, so that the modifiers can be ignored when the script is launched by a keyboard shortcut but can be used normally at other times.

  • rbtwhizrbtwhiz Posts: 2,250
    edited February 2015

    In the next version, you'll be able to use the following example:

    // Define the name associated with this script
    var g_sToolName = "Action Modifier Test";
    
    // Initialize variables that hold modifier key state
    var g_bShiftPressed = false;
    var g_bControlPressed = false;
    var g_bAltPressed = false;
    var g_bMetaPressed = false;
    
    // If the "Action" global transient is defined, and its the correct type
    if( typeof( Action ) != "undefined" && Action.inherits( "DzScriptAction" ) ){
     // If the current key sequence for the action is not pressed
     if( !App.isKeySequenceDown( Action.shortcut ) ){
      // Update variables that hold modifier key state
      g_bShiftPressed = (App.modifierKeyState() & 0x02000000) != 0;
      g_bControlPressed = (App.modifierKeyState() & 0x04000000) != 0;
      g_bAltPressed = (App.modifierKeyState() & 0x08000000) != 0;
      g_bMetaPressed = (App.modifierKeyState() & 0x10000000) != 0;
     }
    }
    
    // Construct debug message
    var sMessage = String("%1 = %2\n%3 = %4\n%5 = %6\n%7 = %8")
     .arg( "g_bShiftPressed" ).arg( g_bShiftPressed )
     .arg( "g_bControlPressed" ).arg( g_bControlPressed )
     .arg( "g_bAltPressed" ).arg( g_bAltPressed )
     .arg( "g_bMetaPressed" ).arg( g_bMetaPressed );
    
    // Display debug info
    MessageBox.information( sMessage, g_sToolName, qsTr("Ok") );

    -Rob

    Post edited by rbtwhiz on
  • Richard HaseltineRichard Haseltine Posts: 100,905
    edited December 1969

    Thank you Rob, I will look forward to that.

Sign In or Register to comment.