Is there an alternative to WPF's "KeyBinding" in UWP? - c#

In my WPF app I am using what's written below to bind a key press of the F10 key to running a method in my script named 'btn_font_click'. However obviously UWP does not support direct keybindings like this in XAML since it's universal and not designed for Windows.
Is there any way that I can get this same effect in a UWP application?
<Window.Resources>
<RoutedUICommand x:Key="cmd1"></RoutedUICommand>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource cmd1}"
Executed="btn_font_Click">
</CommandBinding>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="F10" Command="{StaticResource cmd1}"></KeyBinding>
</Window.InputBindings>
I'm using for inputting data from an RFID reader. Currently when the RFID card is scanned it presses F10, types out its data and then presses enter.
My idea is F10 sets the keyboards focus to a textbox, the script then waits for an enter press while the RFID types out its data and it then takes what's in the text box and splits it into an array for usage within the app.
If there's a better or more direct way for getting the data from within the RFID card to my array I'm open for other potential solutions.
Edit: After looking into this for a while I've found that a 'Keyboard Accelerator' would be best for this programs current functionality as I would like the RFID card to still work while the app isn't in focus. Could I get some pointers as of how to set up a Keyboard Accelerator linking an F10 key press to running my method?

If you want to set up this kind of application-wide keyboard shortcut mechanism, activators are definitely one way to go about doing it.
There is a documentation on keyboard accelerators available and this functionality is available since the Fall Creators Update. Each UIElement has a KeyboardAccelerators collection, which allows you to define keyboard accelerators which interact with it. In case of buttons and menu items invoking the specified shortcut automatically invokes the control, but to make your TextBox focused, you have to specify this behavior yourself using the Invoked event:
<TextBox>
<TextBox.KeyboardAccelerators>
<KeyboardAccelerator Modifiers="None"
Key="F10" Invoked="KeyboardAccelerator_OnInvoked" />
</TextBox.KeyboardAccelerators>
</TextBox>
And in the event handler the TextBox is then focused:
private void KeyboardAccelerator_OnInvoked(
KeyboardAccelerator sender,
KeyboardAcceleratorInvokedEventArgs args )
{
(args.Element as Control).Focus(FocusState.Keyboard);
}
The KeyboardAcceleratorInvokedEventArgs.Element property contains a reference to our TextBox, I cast it to Control, as this is the parent of TextBox that declares the Focus method and you can potentially reuse this method on any focusable control.

Related

Click on a chart and open the same chart in another window with MVVM?

I have a Window in XAML with some different charts. What I wanted to do is to click that chart and open another window that shows me the same chart and another elements added to the GUI, but I donĀ“t know what I am looking to, I am not sure if MVVM would help me or shoukd I start looking for something else?
You are looking for input bindings:
<ChartPart Background="Transparent">
<ChartPart.InputBindings>
<MouseBinding Command="{Binding ShowInAntoherView}"
CommandParameter="{Binding Path}"
Gesture="LeftDoubleClick""/>
</ChartPart.InputBindings>
(Background=Transparent is important, because if the background is null, control does not pass a check if it was clicked)
This allows you invoke commands with parameters on pretty much all controls on a variety of possible user actions.
Just pass a clicked chart viewModel to a command that will navigate to it - done.
What a <ChartPart> is in your case depends on a library you use (in your case it is Livecharts), but it should not be a problem to figure out.

Is there a way to switch between installed windows keyboard layouts in c#?

I have two text-boxes, one for English and one for Russian. For ease of use purposes, I'd like to know if there's something available in c# that tells my system to switch to one of the installed keyboard layouts.
I would then plan to set a method that does this as soon as one of the text-boxes get focused.
So when the Russian box is focused, the windows Russian keyboard layout is used and vice versa.
I was searching online for a bit but I didn't find any of the sort. Since I wanted it finished early I did a workaround and just simulated the key-presses necessary to switch keyboard layouts on windows using Input-simulator. Now I am looking for a better solution.
Public Form1()
{
// I use the method when either of the text-boxes are used.
// When I find a better solution, there will obviously be two separate methods
txtRussian.GotFocus += SwitchKeyboard;
txtEnglish.GotFocus += SwitchKeyboard;
}
private void SwitchKeyboard(object sender, EventArgs e)
{
// shift alt for keyboard layout switch
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT,VirtualKeyCode.LMENU);
// LMENU (Left Alt) tends to still be pressed after you he finished the modified keystroke.
// So that makes any first key the user presses be the "LAlt + {a key}" instead of just a key.
// By normally simulating its press again the issue is gone
sim.Keyboard.KeyPress(VirtualKeyCode.LMENU);
}
Of course, this isn't what I'd truly want, cause whenever you alt tab in and out and refocus on a text-box, it'll just switch to the next keyboard layout installed instead of a specified keyboard layout for which the text-box is meant.
So yeah, is there a way to switch to a specified windows keyboard layout with c#?
There is the "InputLanguage" class in the System.Windows.Forms namespace.
You could use the "InputLanguage.CurrentInputLanguage" property to change the currently used keyboard layout.
There is already another post on stackoverflow about that:
C# - Automatically switch between two different IME in the same keyboard layout
However, you can only change the input language with that, but not the layout inside the language. But I think changing the input language is what you need.
If you also need to change the input layout of the language you could do so with setting the .ImeMode property of the TextBox.

How to use only NumericPin keyboard into all application? UWP

I developing an app in UWP, architecture 64x. With C#, W10 in kiosk mode.
I need to show and use only numeric keyboard for all input's into all application, but the keyboard provided by windows when i use InputScope="NumericPin" in TextBox element the NumericPin keyboard is showed, but when control lose focus for a moment the standard keyboard appears and disappears.
I already tried this, but it works just like what I have explained:
https://msdn.microsoft.com/en-us/library/windows/apps/mt280229.aspx
I also considered use an external keyboard app service and it works perfectly, but my boss say it is the last resource.
http://chessware.ch/virtual-keyboard/index.php
What i looking for is something like:
use the NumericPin keyboard as static in display every time.
the only keyboard layout available be NumericPin keyboard.
I finded this related question but says the same what me:
UWP VirtualKeyboard shows NumericPin layout in simple textbox
Thanks for everybody.
I need to show and use only numeric keyboard for all input's into all application, but the keyboard provided by windows when i use InputScope="NumericPin" in TextBox element the NumericPin keyboard is showed, but when control lose focus for a moment the standard keyboard appears and disappears.
As you said, the keyboard provided by windows and the type of keyboard is determined by InputScope property of input control. You could manually set InputScope property. However, you could not set keyboard for the input control that has not been determined InputScope property.
For your requirement, I think you could set global style for input control in Application.Resources. such as TextBox,RichEditBox etc.
<Application.Resources>
<Style TargetType="TextBox">
<Setter Property="InputScope" Value="NumericPin"/>
</Style>
</Application.Resources>

Standard keyboard shortcuts in WPF controls

I'm interested in a list of all available shortcuts of WPF controls. I was mainly interested in the standard shortcuts for WPF TreeView control (e.g., expand/collapse all, select all and so on), but I can't seem to find any location that lists them. Is there a specific page listing available shortcuts for WPF controls?
I know some shortcuts are supported naively, e.g., Ctrl + A will select all rows in a given ListView control.
I could define my own shortcuts and implement their behaviour. However, I feel it is not a good practice to define shortcuts that are already supported by the .NET framework and hence the need to know about such supported shortcuts.
In other situations I would typically use the same shortcuts available in Visual Studio as it is a WPF application, but I am hopping here for a more extensive list of out of the box supported shortcuts in WPF controls.
Thanks to psoshmo for pointing to the MSDN article. It'd be nice if they were all listed out so you didn't have to comb through a bunch of links to figure it out. So here they are. This is not all the commands, just the once with default key bindings.
Application Commands: ApplicationCommands
ContextMenu: Shift+F10
Copy: Ctrl+C || Ctrl+Insert
Cut: Ctrl+X || Shift+Delete
Delete: Del
Find: Ctrl+F
Help: F1
New: Ctrl+N
Open: Ctrl+O
Paste: Ctrl+V || Shift+Insert
Print: Ctrl+P
PrintPreview: Ctrl+F2
Properties: F4
Redo: Ctrl+Y
Replace: Ctrl+H
Save: Ctrl+S
SelectAll: Ctrl+A
Stop: Esc
Undo: Ctrl-Z
Navigation Commands: NavigationCommands
BrowseBack: Alt+Left
BrowseForward: Alt+Right
BrowseHome: Alt+Home
BrowseStop: Alt+Esc
Favorites: Ctrl+I
Refresh: F5
Search: F3
Media Commands: MediaCommands
None
Component Commands: ComponentCommands
ExtendSelectionDown: Shift+Down
ExtendSelectionLeft: Shift+Left
ExtendSelectionRight: Shift+Right
ExtendSelectionUp: Shift+Up
MoveDown: Down
MoveFocusBack: Ctrl+Left
MoveFocusDown: Ctrl+Down
MoveFocusForward: Ctrl+Right
MoveFocusPageDown: Ctrl+PageDown
MoveFocusPageUp: Ctrl+PageUp
MoveFocusUp: Ctrl+Up
MoveLeft: Left
MoveRight: Right
MoveToEnd: End
MoveToHome: Home
MoveToPageDown: PageDown
MoveToPageUp: PageUp
MoveUp: Up
ScrollPageDown: PageDown
ScrollPageUp: PageUp
SelectToEnd: Shift+End
SelectToHome: Shift+Home
SelectToPageDown: Shift+PageDown
SelectToPageUp: Shift+PageUp
Editing Commands: EditingCommands
AlignCenter: Ctrl+E
AlignJustify: Ctrl+J
AlignLeft: Ctrl+L
AlignRight: Ctrl+R
Backspace: Backspace
DecreaseFontSize: Ctrl+OemOpenBrackets
DecreaseIndentation: Ctrl+Shift+T
Delete: Delete
DeleteNextWord: Ctrl+Delete
DeletePreviousWord: Ctrl+Backspace
EnterLineBreak: Shift+Enter
EnterParagraphBreak: Enter
IncreaseFontSize: Ctrl+OemCloseBrackets
IncreaseIndentation: Ctrl+T
MoveDownByLine: Down
MoveDownByPage: PageDown
MoveDownByParagraph: Ctrl+Down
MoveLeftByCharacter: Left
MoveLeftByWord: Ctrl+Left
MoveRightByCharacter: Right
MoveRightByWord: Ctrl+Right
MoveToDocumentEnd: Ctrl+End
MoveToDocumentStart: Ctrl+Home
MoveToLineEnd: End
MoveToLineStart: Home
MoveUpByLine: Up
MoveUpByPage: PageUp
MoveUpByParagraph: Ctrl+Up
SelectDownByLine: Shift+Down
SelectDownByPage: Shift+PageDown
SelectDownByParagraph: Ctrl+Shift+Down
SelectLeftByCharacter: Shift+Left
SelectLeftByWord: Ctrl+Shift+Left
SelectRightByCharacter: Shift+Right
SelectRightByWord: Ctrl+Shift+Right
SelectToDocumentEnd: Ctrl+Shift+End
SelectToDocumentStart: Ctrl+Shift+Home
SelectToLineEnd: Shift+End
SelectToLineStart: Shift+Home
SelectUpByLine: Shift+Up
SelectUpByPage: Shift+PageUp
SelectUpByParagraph: Ctrl+Shift+Up
TabBackward: Shift+Tab
TabForward: Tab
ToggleBold: Ctrl+B
ToggleBullets: Ctrl+Shift+L
ToggleInsert: Insert
ToggleItalic: Ctrl+I
ToggleNumbering: Ctrl+Shift+N
ToggleSubscript: Ctrl+OemPlus
ToggleSuperscript: Ctrl+Shift+OemPlus
ToggleUnderline: Ctrl+U
Update 2016-01-12: I missed the Editing Commands as they are referenced in another place. The article for them also lists out the default key bindings, which can be found here.
Update 2018-04-16: Added Editing Commands and added links.
As far as I know, there is no master list of supported shortcuts, and you are right that they in general will inherit their shortcuts from the basic shortcuts in Windows. I have searched for this on my own before and have never been able to locate a list. Others I have seen asking on the internet have also never had any luck.
So the unfortunate answer is: No, there is no list.
EDIT: OK, after some digging, the best I can give you is a list of pre-defined commands that you can apply to your controls that are built into WPF. These come in five categories:
Application Commands
Navigation Commands
Component Commands
Media Commands
Editing Commands
Their documentation can be found on MSDN here (an example). Clicking on one of the commands will take you to its individual page which will list their default shortcut like this as its "Key Gesture" property.
You can assign these default commands to controls like so:
<Button Command="ApplicationCommands.Cut" CommandTarget="{Binding ElementName=txtEditor}" Width="60">_Cut</Button>
<Button Command="ApplicationCommands.Paste" CommandTarget="{Binding ElementName=txtEditor}" Width="60" Margin="3,0">_Paste</Button>
This is a seemingly pretty basic tutorial on binding these commands to buttons and what not (where that code is from). This doesn't tell you what commands are there by default, but it should be able to at least deduce the keyboard shortcuts for some of these commands, for whatever that's worth.

Is there a way to press or fireup the keys in key board using c# code?

I want the 'Alt' to be pressed by code. It is like firing key-press event with key 'Alt' by the code, not hitting it manually. The need is, I have set 'ShortCut keys for menu, but it (the single underline on key letter) is not visible to the user unless he presses 'alt'. So i need to make the Alt be pressed by default.
Is there a way to 'press' or 'fireup' the keys in key board using c# code?
Check out the System.Windows.Forms.SendKeys class.
You can use the static Send method to send keystrokes to the active window. If you're trying to send keystrokes to another window, you'll need to use the Windows API to activate the other window first.
If you have any control over the operating system on which the program is being deployed, apparently you can force the underlined shortcut letter to always be displayed by going to Control Panel -> Display -> Appearance -> Effects -> Hide underlined letters for keyboard navigation.
(http://www.chinhdo.com/20080902/underlined-letters-windows/)
here is a complete article on use of SendKeys on codeproject
Here is how you simulate input (both mouse and keyboard).
http://msdn.microsoft.com/en-us/library/ms171548.aspx
If you look at the System.Windows.Forms.SendKeys class you will see that it provides you with what you want.
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

Categories

Resources