In the last bird text, the old week through the unprecedented code to show you the use of access keys (that is, Alt + xxx). However, the big partners will certainly find that access keys, after all, the restrictions are large, not too flexible, and not good to play, so you need to customize the shortcut keys.
In fact, the custom shortcut key is not what very mysterious things, the old week always do not like the trick, plainly, is the handling of keyboard events. The UWP has similarities to WPF, but there are differences. These differences do not affect our programming, if there are some differences you feel difficult, it proves that your programming level is too poor, hurriedly go home to the party organization to write 10 copies of 800000 words of the review.
Since it's a keyboard event, you first close your eyes, and while you're resting, think about what related data the keyboard event will involve. First of all, the program must know which key is manipulated, yes, and each key has built-in code, such as virtual key code, this good, Windows.System.VirtualKey enumeration has been defined for us, so this you do not have to worry about, then we will think, a key will have what to do? Yes, nothing more than press and release, that is, key down and key up.
As long as you have figured out the above questions, it's a good thing to do, and it's easier than eating chestnuts.
The UWP SDK has two keyboard events for us: KeyDown occurs when the key is pressed, and KeyUp occurs when the key bounces. As for which event to deal with, or whether it is handled by two, it depends on what you are going to do.
Note that these two keyboard events occur in two places:
1. The UIElement class exposes these two events, which are used to handle keyboard behavior of elements on the user interface, but do not include the current window.
2. To handle keyboard events at the current window level, use the CoreWindow class, which also exposes these two keyboard events. The Getforcurrentthread static method is called to get an instance of the current window.
Part 1: Simple key handling
The so-called simple button, just press a key shortcut key. Here I use an example to illustrate.
There is a ListView control on the interface with four options in it, and the XAML is as follows.
<ListViewName= "LV"Margin= "+"> <ListViewItem>Plane</ListViewItem> <ListViewItem>Train</ListViewItem> <ListViewItem>Bike</ListViewItem> <ListViewItem>Bus</ListViewItem> </ListView>
Subsequently, we implemented the function, using F1, F2, F3 and F4 four keys respectively to select the item in the list control above. If F1 Select the first item, F2 Select the second item, and so on.
Here, the old week selects the CoreWindow class, that is, the shortcut key can be captured within the scope of the current window.
CoreWindow Corewind =NULL; ... corewind=Corewindow.getforcurrentthread (); Corewind.keydown+=Onwindowkeydown; ...Private voidOnwindowkeydown (CoreWindow sender, KeyEventArgs args) {Switch(args. Virtualkey) { CaseWINDOWS.SYSTEM.VIRTUALKEY.F1:LV. SelectedIndex=0; Break; CaseWINDOWS.SYSTEM.VIRTUALKEY.F2:LV. SelectedIndex=1; Break; CaseWINDOWS.SYSTEM.VIRTUALKEY.F3:LV. SelectedIndex=2; Break; CaseWINDOWS.SYSTEM.VIRTUALKEY.F4:LV. SelectedIndex=3; Break; } }
The above code is very understood, it is not explained here, save 600 words.
Part 2: Compound accelerator processing
The so-called compound shortcut key, is at the same time press two or more than two keys, we say less p, or through examples to illustrate it. The example is this: Use the image control on the page to display a picture, then press CTRL + Plus to enlarge the image, and press CTRL + MINUS to zoom out.
The main XAML is as follows.
<ImageSource= "Assets\1.jpg"Margin= "6"Rendertransformorigin= "0.5,0.5" > <Image.rendertransform> <ScaleTransformx:name= "SCL"ScaleX= "1.0"ScaleY= "1.0"/> </Image.rendertransform> </Image>
Because the Image control cannot receive keyboard input focus, the element KeyDown and KeyUp events do not respond, or the events on the Corewindow class are considered.
NULL ; protected Override void onnavigatedto (NavigationEventArgs e) { = corewindow.getforcurrentthread (); + = OnKeydown ; + = OnKeyup; }
This is specifically to declare a variable of type bool, which is used to indicate whether the CTRL key is pressed.
BOOL false;
If you do not want to use a variable to flag whether the CTRL key is pressed, you can also access the CoreWindow object's Getkeystate (Windows.System.VirtualKey) method to check if the CTRL key is pressed. However, it is still easier to declare a variable directly to save the key state.
In KeyUp event handling, if the CTRL key bounces, let the Ctrl_down variable change to false.
Private void OnKeyup (CoreWindow sender, KeyEventArgs e) { if (E.virtualkey = = Virtualkey.control ) {false; } }
In the KeyDown event, there are two situations: A, if you press the CTRL key, change the variable ctrl_down variable to true;b, if the CTRL key is pressed, and also press the other keys, it is necessary to determine whether the plus or minus key is pressed. Plus key zooms in on the image and the minus key shrinks the image.
Private voidOnKeydown (CoreWindow sender, KeyEventArgs e) {if(E.virtualkey = =Virtualkey.control) {Ctrl_down=true; return; } //to scale processing if(ctrl_down) {Switch(e.virtualkey) { CaseVirtualkey.add:if(SCL. ScaleX <5.0) SCL. ScaleX + =0.2; if(SCL. ScaleY <5.0) SCL. ScaleY + =0.2; Break; Casevirtualkey.subtract:if(SCL. ScaleX >0.3) SCL. ScaleX-=0.2; if(SCL. ScaleY >0.3) SCL. ScaleY-=0.2; Break; } } }
Now run the sample and press the CTRL + (+) and CTRL + (-) keys yourself to try.
Please pay serious attention to: The key event does not record the ALT key, which is represented in the Virtualkey enumeration with Menu, this guy is more alternative. It can be obtained from the Ismenukeydown field of the corephysicalkeystatus structure that represents the key state.
The two sample source code for this article
"Windows 10 App development" custom shortcut keys