ToolStripMenuItem shortcut keys do not ender line with mouse click - c#

I have a strange problem. I have a toolStripMenuItem that I wish to have the first letter underlined. This is in Windows Forms VS 2010.
It is defined in the designer with text equal to "&Expand" and show shortcut keys = true and shortcut keys = Alt+E. It shows up in the form in designer as expected with the E underlined.
I have a click event that changes the name, and then does something. If you click again it sets the name back
private void expandToolStripMenuItem_Click(object sender, EventArgs e)
{
if (expandToolStripMenuItem.Text == "&Expand")
{
expandToolStripMenuItem.Text = "Collaps&e";
expandToolStripMenuItem.ShortcutKeys = Keys.Alt | Keys.E;
// execute some other code
}
else
{
expandToolStripMenuItem.Text = "&Expand";
expandToolStripMenuItem.ShortcutKeys = Keys.Alt | Keys.E;
}
}
I also set the &Expand in the form shown and load events although this makes no difference.
Now when the form opens the E is not underlined. If I click it with the mouse it shows Collapse but the e is not underlined. I can go back and forth with the mouse and the letters are never underlined. However if I type AltE, the letter is underlined in both cases from that point on. Even with the mouse.
Any ideas?
Thanks

After my comment, I looked a little deeper. This is expected functionality and is configurable by the user.
In the Windows Control Panel in the Ease of Access Center, there is an option in the "Make the keyboard easier to use" section called "Make it easier to use keyboard shortcuts: Underline keyboard shortcuts and access keys". If that check box is unchecked the system will only underline access keys (the proper name for the shortcut underlining which you're dealing with) when the user is already navigating with access keys (using Alt). If the check box is checked then they will always be underlined.
You can test this with the file menu in pretty much any application in Windows.

Related

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 simulate keystroke in WPF, but outside the application?

Currently I used this snip code as a result from googling.
var eventArgs = new TextCompositionEventArgs(Keyboard.PrimaryDevice,
new TextComposition(InputManager.Current, Keyboard.FocusedElement, "A"));
eventArgs.RoutedEvent = TextInputEvent;
var flag = InputManager.Current.ProcessInput(eventArgs);
It was working if I used Keyboard.Focus(TxtBox); and the TxtBox will be filled with the Keystroke.
But what I want really achieved is:
1.Drawing a box (for example, I draw box on one of the excel cell)
2.Click on the box coordinate (to change Keyboard Focus)
3.Send Keystroke to clicked excel cell
I have done step 1 and 2.
But I can't find a way to do the third step.
Somehow, the click event (using mouse event) maybe not changing Keyboard Focus automatically.
So, how do I change Keyboard focus, if possible using coordinate ?
Or maybe can I get IInputElement from a coordinate ? and then set keyboard focus to it.
Of course, all of it outside from the main application window of the WPF.
Found it !
At:
Installed InputSimulator via NuGet, no members accessible
It is working in most cases.
I said in most cases, because it is able to type in other window like excel application, but on other custom app window. There might be a case it won't work.
Hope it help for other people, looking for the same thing.

How to prevent auto closing of soft input panel in UWP?

I am facing an unexpected behavior of Keyboard showing and hiding in UWP app working on tablet with windows 10.
After carefully testing again and again i noticed that this issue comes when you have focus on input box and keyboard is opened for it. Now focusing next input needs layout adjustment so that it should not be hidden by keyboard. When you try to focus next element, by default previously opened keyboard hides and now i'm not able to open keyboard un till this new input box lose focus and manually gain focus again.
So for controlling this issue i want to prevent automatic hide and show of keyboard every time i switch focus to new textbox. It should open keyboard once the page load (already found solution using InputPane) and hiding should only be by clicking cancel (x) button.
Please check this video for clearly understanding the issue.
https://www.dropbox.com/s/1c876uwytywio1t/Soft%20Keyboard%20Issue.mp4?dl=0
Please vote this suggestion if anyone else is also facing this issue.
https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/34170142-inputpane-does-not-open-when-focus-is-shifted-to-n
This issue has been partially resolved in windows 10 version 1803 released on 30th April, 2018. In this version InputPane does not hide and show again and again when focus is shifted from one input element to other.
You may try to put a horizontal-stretched placeholder control (let say, StackPanel) at the bottom of your page, then let it the same size the on-screen keyboard does. That could prevent uncontrolled auto-hide trigger being fired (at least I did that trick on UWP mobile app):
// on the window initialization, remember the input pane
this._inputPane = InputPane.GetForCurrentView()
// then, subscribe to the events
_inputPane.Showing = (sender, args) =>
{
args.EnsuredFocusedElementInView = true; // skip default vertical-shift behavior
this._placeholderPane.Height = args.OccludedRect.Height;
}
_inputPane.Hiding = (sender, args) =>
{
this._placeholderPane.Height = 0;
}
Hope it helps on the Win10 desktop same way as it was on mobile one.
P.S. Yes, initially the placeholder pane is zero-height and collapsed.

.Net Toolstrip/MenuStrip Focus Issues

No matter what the scenario may be I'm able to recreate this annoying problem 100% of the time.
Create a .Net project, C# or VB.Net. Add a ToolStrip control to the form. Create a few simple DropDownButton(s) that contain at least 2 menu items. Add any other controls you wish, a list box (populate it so it can receive focus correctly) and a ComboBox control. Either assign shortcut keys or enable TabStop on the ToolStrip so that it can receive focus by Keyboard.
Run the project (Debug/Release, which ever you fancy). Use your Keyboard to give the ToolStrip Focus (by tab or shortcut key). Arrow down into a sub item. Now select the escape key to collapse the Toolstrip sub menu. Tab to the ListBox or ComboBox that contains a few Items.
All looks great right? Now use your arrow keys to navigate in these controls... Surprise! your back on the ToolStrip and the control you thought had focus doesn't!
I've tried multiple things to force focus on the ListBox. One example is I'd add the event handler for OnEnter (ListBox.Enter+=...) and add some code like:
ListBox.Focus();
ListBox.Select();
Nothing was a success... It seems like once the menu expands on a toolstrip you will be forever stuck on this control using your Keyboard...
This is important for me to resolve due to the fact that i work with blind users whom use keyboard navigation only... Is this a bug? I cannot reproduce this in MFC...
Any suggestions?
Update
I was able to find a control that doesn't reproduce this strangeness...
System.Windows.Forms.MainMenu is the only "Tool Bar object" that doesn't behave like the others...
I'd still like some feedback on the above though (Help for others and myself)...
Update 2
The underlying issue is within [ToolStripObject].TabFocus property... if set to false all seems to work ok... giving focus back to the control that "looks" like it's focused. But having that capability to allow a blind user to navigate throughout all UI controls via tab is a handy thing to implement... it's too bad this property doesn't work like it should....
I got it to work by overriding the ToolStripMenuItem:
public class ToolStripMenuItemEx : ToolStripMenuItem {
protected override bool ProcessCmdKey(ref Message m, Keys keyData) {
if (keyData == Keys.Escape) {
ToolStripDropDownButton tb = this.OwnerItem as ToolStripDropDownButton;
if (tb != null) {
tb.HideDropDown();
return false;
}
}
return base.ProcessCmdKey(ref m, keyData);
}
}

Underline letter in a button

In my form, I have four buttons that do different tasks. The
button.Text field is set as &Next Project, so the N will be underlined.
In the preview of the form, it shows up with the N underlined. Yet
when I run my program, the text shows up with no underlining.
Press Alt.
Those keyshortcuts are only working in combination with the Alt-Key, and therefor only show up if it is pressed.
Also there seem to be two ways to always show the Mnemonics:
Change the Windows Desktop/Theme Settings:
Right Click Desktop -> Properties
Got to the Appereance Tab -> Effects
Uncheck "Hide Underlined letters for keyboard navigation until I press the ALT Key"
Change the System-Parameters via an API-Call.

Categories

Resources