Login Page - Create Account

Support Board


Date/Time: Sat, 23 Nov 2024 23:16:32 +0000



Post From: Using Keyboard Shortcuts in ACSIL

[2019-03-08 03:55:02]
Yoda - Posts: 106
I was able to get the shortcut keys working with ASCIL and provide some comments below to help anyone reading this.

As an aside, I found the documentation a bit lacking on this topic and thought it might be helpful if SC included some of the following in the documentation.
I think it will also be immensely helpful if there were some coding examples. Accordingly, I have included a small script that I used when trying to figure out the shortcut keys. Please feel free to use parts or all of the script in the SC documentation.

Summary of findings (see script below for actual examples):

1. If a single key is pressed, i.e. the letter e, it appears that you can use sc.KeyboardKeyEventCode and a Windows Virtual Key Code number.
You can also use sc.CharacterEventCode and an ASCII number. You cannot use ASCII with sc.KeyboardKeyEventCode nor a Windows Virtual Key Code number with sc.CharacterEventCode.

2. It also appears that when trying to use a combination of keys (i.e. CTRL E) sc.KeyboardKeyEventCode and Windows Virtual Key Code number will NOT work.
For key combinations, one must use sc.CharacterEventCode and the ASCII value.

3.   Not all key combinations will work. Specifically, if the combination is already assigned i.e. CTRL S Which is assigned to File >> Save, it will not work.
  Trying to figure out which combinations are in use is a bit of a pain. Ideally, it would be nice if there was a way to print out the SC Keyboard Shortcuts with assigned values. Maybe SC will consider providing this option in a future release.
  In the meantime, the only way I could figure out to determine if a key combination is used, is to open up the Global Settings >> Customize Keyboard Shortcuts and select a key combination. Then see if something appears in the Key Currently Used By box.
  
4. I found that one has to be careful with different key combinations. For example: CTRL SHIFT E will correctly not trigger if you just use CTRL E. However, CTRL E and CTRL SHIFT E will BOTH trigger if CTRL SHFIT E is pressed. In other words, it appears that the SHIFT is ignored. See the code below for a specific example.

5. You can determine the ASCII and Window Virtual Key numbers by loading the SC provided custom study called Receive Keyboard Events Example.

Finally, here is the script. I hope this all helps and saves someone a lot of time.



/*=============================================================================
  *** Prepared for educational purposes only, see terms at end of code ***
  
  File: Keyboard-Test-v01a-20190307-2120.cpp
  Date last updated: 2019-03-07 2120

  NOTES:
  If a single key is pressed, it appears that one can use sc.KeyboardKeyEventCode
and the Windows Virtual Key Code number.

However, it appears that when trying to use a combination of keys, i.e. CTRL E
sc.KeyboardKeyEventCode and Windows Virtual Key Code number will NOT work.
For combinations, one must use sc.CharacterEventCode and the ASCII value.

See below for examples.
*/

#include "sierrachart.h"

SCDLLName("Keyboard Test v01a 03-07 2120 T4p DLL")
  
SCSFExport scsf_KeyboardTest01aT4p(SCStudyInterfaceRef sc)
{
  sc.GraphName = "Keyboard Test";

  sc.StudyDescription = "";

  sc.GraphRegion = 0;    

  sc.AutoLoop = 0;
    
  sc.ReceiveKeyboardKeyEvents = 1;

  sc.ReceiveCharacterEvents = 1;

  sc.SupportKeyboardModifierStates = 1;


  int PressedKey = sc.KeyboardKeyEventCode;
  int PressedChar = sc.CharacterEventCode;

  
  if ((PressedChar == 101)) // E = ASCII value of 101

  {
    SCString Message;

    Message.Format("sc.CharacterEventCode method - ASCII: E ( %d ) was pressed", PressedChar);        

    sc.AddMessageToLog(Message, 1);    
  }
  
  if ((PressedKey == 69)) // E = Windows Virtual Key value of 69

  {
    SCString Message;

    Message.Format("sc.KeyboardKeyEventCode metod - Windows Virtual Key: E ( %d ) was pressed", PressedKey);        

    sc.AddMessageToLog(Message,1);    
  }

  
  if ((sc.IsKeyPressed_Control && sc.IsKeyPressed_Shift && PressedChar == 5))

  {
    SCString Message;

    Message.Format("Control, Shift and E ( %d ) were pressed", PressedChar);        

    sc.AddMessageToLog(Message,1);    
  }

  // NOTE: The following will trigger even if you use another key in the combination.
  // i.e. When you press CTRL SHIFT E, the above if stmt triggers AND so does the one below.
  // Also note that you do not need to include sc.IsKeyPressed, only the ASCII code.
  
  if ((PressedChar == 5)) // CTRL E = ASCII value of 5

  {
    SCString Message;

    Message.Format("CTRL E ( %d ) were pressed", PressedChar);        

    sc.AddMessageToLog(Message,1);    
  }



// ****************** DISCLAIMER **********************************************
//
// This code is provided for educational purposes on an As Is, Where Is basis.
// No express or implied warranties are provided.
// There may be errors in this code, and it must not be used for live trading.
// Trading is very risky and you can lose more than you have invested.
// Past performance is not necessarily indicative of future results.
// By using any of this code, you acknowledge & agree to the above.
//
//=============================================================================
}