Login Page - Create Account

Support Board


Date/Time: Sun, 08 Sep 2024 00:39:42 +0000



[Programming Help] - Keyboard Shortcut: Ctrl + Number

View Count: 667

[2022-12-22 23:29:08]
ondafringe - Posts: 273
Anyone know why this doesn't work?

if (sc.SetDefaults)
{
sc.ReceiveKeyboardKeyEvents = true;
sc.ReceiveCharacterEvents = true;
sc.SupportKeyboardModifierStates = true;

return;
}

int pressedCharacter = sc.CharacterEventCode;

if (sc.IsKeyPressed_Control && pressedCharacter == 52) // number 4
{
//some code  
}


pressedCharacter = 0;


If I just check for sc.IsKeyPressed_Control, it works.

If I just check for pressedCharacter, it works.

But if I check for both, it doesn't work. I've tried using different characters instead of a number, but still can't get it to work.

Any ideas?
[2022-12-24 03:28:56]
ondafringe - Posts: 273
According to Global Settings > Customize Keyboard Shortcuts:

Ctrl + NumPad Number is not being used by SC, and I am using the Number Pad.

And Shift + Any Number is not being used by SC, either.

So I tried:

if (sc.IsKeyPressed_Shift && pressedCharacter == 52) // ASCII Number 4
{
//some code
}

and I tried:

if (sc.IsKeyPressed_Control && sc.IsKeyPressed_Shift && pressedCharacter == 52) // ASCII Number 4

{
//some code
}

... and it still doesn't work.

In fact, even with NumLock on:

Shift + 2 and Ctrl + Shift + 2 maps to the down-cursor key

and:

Shift + 8 and Ctrl + Shift + 8 maps to the up-cursor key

...even though "Global Settings > Customize Keyboard Shortcuts" doesn't show those key combos are being used by SC. Why do that? Doesn't make sense to me, but I guess it really doesn't matter because nothing else works anyway.

And, again, I can check for the individual keys separately, and it works, but the key combos are DOA.

According to the following old support thread, there is a custom study called "Keyboard Receive Event Examples," but I can't locate that study. And the trader in that old support thread appears to have solved the problem. I think I am doing it the way that trader is doing it, so it's confusing as to why it still doesn't work for me.

Using Keyboard Shortcuts in ACSIL

With all the traders having problems getting this to work over the years, it seems like SC would have better documentation, along with examples, by now. Maybe they do and I just can't find what I'm looking for. Seriously, if SC can get key combinations to work, we should be able to get them to work, as well. So, IMO, it can't be the OS.

I'll keep using my work-around while I try to figure this out, but it's not an ideal solution.
[2022-12-24 15:12:23]
ondafringe - Posts: 273
It looks like I have finally figured this out, so let me explain, in hopes it will help others in the future who might be dealing with key-combo shortcut issues.

Up in the Set Defaults section of the study, I included:

sc.ReceiveKeyboardKeyEvents = true;
sc.ReceiveCharacterEvents = true;
sc.SupportKeyboardModifierStates = true;

I had already done that part.
    
Then somewhere below and outside the Set Defaults section, I included:

pressedKey = sc.KeyboardKeyEventCode;
pressedCharacter = sc.CharacterEventCode;
  
I had already done that part, as well.

According to "Global Settings > Customize Keyboard Shortcuts," SC is not using the numeric keypad numbers (on the right side of the keyboard) for anything, but they are using the numbers across the top of the keyboard.

This is where the confusion came in, and here is what I finally realized:

pressedKey is related to Windows key codes.

pressedCharacter is related to ASCII key codes.

I want to use numeric keypad numbers to change my target and stop on the fly, using numeric keypad numbers by themselves to change the target, and using Ctrl + numerice keypad numbers to change the stop.

ASCII code doesn't differentiate between the numbers across the top of the keyboard and the numbers on the numeric keypad. The numbers I am interested for targets and stops are 1-5, ASCII code 49-53.

So this code to change the target to 4 worked:

if (pressedCharacter == 52) // any number 4
{
  //execution code
}

However, this code to change the stop to 4, didn't work:

if (sc.IsKeyPressed_Control && pressedCharacter == 52) // any number 4
{
  //execution code
}

The reason it didn't work is because ASCII 52 is number 4, and it didn't matter whether I pressed the number on the top of the keyboard *OR* I pressed the number on the numeric keypad. Both numbers registered as ASCII 52. And since SC is using the numbers across the top of the keyboard, my code was ignored.

pressedKey is different. It uses the Windows Key event and that event *DOES* differentiate between the numbers across the top of the keyboard and the numbers on the numeric keypad. Since the Windows Key events are in hex, I had to convert the numeric keypad numbers 1-5 (the numbers I am interested in) from hex to decimal, which gave me 97-101.

So this code to change the stop to 4 works:

if (sc.IsKeyPressed_Control && pressedKey == 100) // numeric keypad number 4
{
  //execution code
}

Now in the link I posted to that old support thread, that trader indicated the above key combo wouldn't work, and that was partly what was causing confusion. So what that trader said did work back then, doesn't work now, and what that trader said didn't work back then, does work now. Or else I am reading that thread wrong.

So now I am using pressedCharacter to change my targets, and I am using Ctrl + pressedKey to change my stops, all on the fly, and it appears to be working. However, I can't use pressedCharacter by itself to change my target and use pressedKey by itself to change my stops because that would change both target and stop to the same number.

Here are the Windows Key Codes:

https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

Here are the ASCII codes:

https://www.ascii-code.com/

Here is a hex-to-decimal converter:

https://www.rapidtables.com/convert/number/hex-to-decimal.html
Date Time Of Last Edit: 2022-12-24 16:29:13
[2022-12-31 07:15:34]
User183724 - Posts: 183
thx for posting this
[2024-07-31 02:33:24]
ondafringe - Posts: 273
Just to update this: I ran into some problems using both pressedCharacter and pressedKey, so I now only use pressedKey.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account