Is there a way to detect pressed key combo without altering the original key without modifiers?
-
Hi,
So basically if I press 1, I get uievent.key as 1. If I hold Ctrl, I get "Ctrl+1", if I hold shift, I get "Shift+!". That makes my hotkey system complicated.
I allow users to define hotkeys per original key like 1, and add more customization based modifiers. Is it possible to get the actual key without the Shift modifying it?
Is there a way to access this info, perhaps asking qt what key press would be sent if I didnt held down Shift?
Thanks a lot.
-
Hi,
So basically if I press 1, I get uievent.key as 1. If I hold Ctrl, I get "Ctrl+1", if I hold shift, I get "Shift+!". That makes my hotkey system complicated.
I allow users to define hotkeys per original key like 1, and add more customization based modifiers. Is it possible to get the actual key without the Shift modifying it?
Is there a way to access this info, perhaps asking qt what key press would be sent if I didnt held down Shift?
Thanks a lot.
So you want to define a key combo, where it ignores the modifiers?!
Have a look at this (https://doc.qt.io/qt-5/qkeyevent.html#modifiers).
You could write an EventFilter to get the "modified" key. -
@Pl45m4 said in Is there a way to detect pressed key combo without altering the original key without modifiers?:
modifiers
Yes basically the app I am using passes me the key that was pressed but also the state of modifiers and mouse buttons. That's easy to get. But when I ask what key was pressed it not only includes the modifier keys in the string, but also with Shift, it alters the key that was pressed.
So Shift + 1 becomes, Shift+!, but I want to only get 1, no matter the modifiers so I can respond based on my query of the modifier keys, but I don't want to handle shift differently. My code logic is like this:
If key == 1
if ctrl is pressed ...
if shift is pressed ...But if shift alters the key string, now I have to handle ! etc too, but also every region will have different variations.
I don't want to do that.
-
@Pl45m4 said in Is there a way to detect pressed key combo without altering the original key without modifiers?:
modifiers
Yes basically the app I am using passes me the key that was pressed but also the state of modifiers and mouse buttons. That's easy to get. But when I ask what key was pressed it not only includes the modifier keys in the string, but also with Shift, it alters the key that was pressed.
So Shift + 1 becomes, Shift+!, but I want to only get 1, no matter the modifiers so I can respond based on my query of the modifier keys, but I don't want to handle shift differently. My code logic is like this:
If key == 1
if ctrl is pressed ...
if shift is pressed ...But if shift alters the key string, now I have to handle ! etc too, but also every region will have different variations.
I don't want to do that.
If you check out the way key codes are handled in .NET you can see it returns the key itself separately from the modifiers:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { // Determine whether the key entered is the F1 key. If it is, display Help. if(e.KeyCode == Keys.F1 && (e.Alt || e.Control || e.Shift)) { // Display a pop-up Help topic to assist the user. Help.ShowPopup(textBox1, "Enter your name.", new Point(textBox1.Bottom, textBox1.Right)); } else if(e.KeyCode == Keys.F2 && e.Modifiers == Keys.Alt) { // Display a pop-up Help topic to provide additional assistance to the user. Help.ShowPopup(textBox1, "Enter your first name followed by your last name. Middle name is optional.", new Point(textBox1.Top, this.textBox1.Left)); } }
This is exactly what I wanna do. Right now I figured out how to send the key back if shift is pressed using the sendEvent function and it works great except that function is also looking for the key as a string, which if I provide will be the altered key string not the original key, i.e. ! instead of 1.
I am not sure how to access the key before the modifiers were pressed.
-
What you want is imo not possible since it depends on the keyboard layout. '1 + Shift' is not always '!'. And your .NET code also does not show that it's possible with .NET.
-
With .NET it's possible but you are thinking about it wrong.
I am not looking to respond to !, but 1.
So in my hotkeys if there is an action for Shift+1, it will always be 1 with Shift key held down.
So in .NET I can just check if 1 and Shift is allocated for a valid action. I don't have to deal with the shifted characters. All I care is the unshifted key and the modifiers that act on top of the main key.
This already works with Ctrl and Alt, but Shift changes the key name so my app can not find the right action that's on the key i.e. 1.
-
@lachdanan said in Is there a way to detect pressed key combo without altering the original key without modifiers?:
With .NET it's possible
Please show us the .NET code.
And since you know where the action is coming from - why do you need to know it at all?
-
@Christian-Ehrlicher said in Is there a way to detect pressed key combo without altering the original key without modifiers?:
@lachdanan said in Is there a way to detect pressed key combo without altering the original key without modifiers?:
With .NET it's possible
Please show us the .NET code.
And since you know where the action is coming from - why do you need to know it at all?
It's just like in the above code:
You respond by the unshifted key:
if(e.KeyCode == Keys.D1)
then you perform an operation based on the active modifiers.What do you mean by "And since you know where the action is coming from - why do you need to know it at all?"?
I have a dictionary for all primary keys without the modifiers, so if 1 is pressed, I look up the list of available actions on that key based on rules and modifiers is a way to assign more actions to a single key that way. But because 1 becomes !, I don't have any actions stored for that key.
In .NET, you pass the key code which will ALWAYS be 1, and from there it's up to me how to interpret the modifiers. I am not being passed ! where I need to figure out the unshifted key.
-
@Christian-Ehrlicher said in Is there a way to detect pressed key combo without altering the original key without modifiers?:
@lachdanan said in Is there a way to detect pressed key combo without altering the original key without modifiers?:
With .NET it's possible
Please show us the .NET code.
And since you know where the action is coming from - why do you need to know it at all?
It's just like in the above code:
You respond by the unshifted key:
if(e.KeyCode == Keys.D1)
then you perform an operation based on the active modifiers.What do you mean by "And since you know where the action is coming from - why do you need to know it at all?"?
I have a dictionary for all primary keys without the modifiers, so if 1 is pressed, I look up the list of available actions on that key based on rules and modifiers is a way to assign more actions to a single key that way. But because 1 becomes !, I don't have any actions stored for that key.
In .NET, you pass the key code which will ALWAYS be 1, and from there it's up to me how to interpret the modifiers. I am not being passed ! where I need to figure out the unshifted key.
@lachdanan
can you show use your Qt code?and is this for QWidgets or for QML ?
Beaus eI have the following shortcut in QML
Shortcut{ sequence: "Shift+1" onActivated: { console.log("Shift+1 pressed at once") } }
And it's working fine, 1 is registered as 1 and not as the shifted char, ! in my keyboard layout
-
@lachdanan
can you show use your Qt code?and is this for QWidgets or for QML ?
Beaus eI have the following shortcut in QML
Shortcut{ sequence: "Shift+1" onActivated: { console.log("Shift+1 pressed at once") } }
And it's working fine, 1 is registered as 1 and not as the shifted char, ! in my keyboard layout
@J-Hilk
I am using the at interface of the app from python with pyside2. This is what my app provide wrt key and mouse actions:
https://www.sidefx.com/docs/houdini/hom/network.html
They allow you to interpret your own actions if you choose so, and that's what I am trying to do.
When I query the key value which is a string I get there shifted value.
-
It's simply not possible with Qt since there is nothing globally defined - at first the keyboard layout is not the same everywhere and second the underlying OS may already give you the composed character.
-
It's simply not possible with Qt since there is nothing globally defined - at first the keyboard layout is not the same everywhere and second the underlying OS may already give you the composed character.
@Christian-Ehrlicher thanks for confirming.