my question is kinda simple but I didn't find any answer.
I have a WPF application and I want to remap the behaviour of Tab and Enter keys onto other keys, because I need them to be close on the keyboard.
I already managed to make a letter key (for example J) acting like Tab Key, but I can't find a way to make a close button (for example K or H) acting like Enter!
For instance, I need this because the application works only with keyboard, so when an element (for example a button) has focus, I would like to press K or H to act like Enter key and do something. All works fine if I press the real Enter key but as I said, I would like to remap this function to another Key.
Is this possible? If so, how to do it in a WPF application?
Related
I would like to be able to allow users of my application to press Ctrl+Shift+numeric keypad keys to act as a navigational shortcut in an application I am developing. To my surprise it appears to be impossible to recognise this key combination.
Pressing Shift+keypad activates the extended functions displayed on most keyboards, such as cursor keys (on 2, 4, 6 and 8), page down/up (on 3 and 9), end/home (on 1 and 7), etc.
The various key events in WinForms report these exactly as if they were the actual cursor keys etc. being pressed. I'm therefore unable to distinguish between e.g. Shift+KeyPad4 and CursorLeft. I can't simply look for Ctrl+CursorLeft instead of Ctrl+Shift+KeyPad4, as people may be using Ctrl+CursorLeft for text editing purposes.
Is there any way I can properly detect these shifted numeric keypad keyboard combinations?
Thanks!
Use a class to keep track of what keys are pressed.
When you receive an OnKeyDown event, store that key as down and then when you come to When you receive an OnKeyUp event, remove that key from the store.
Then on the same OnKeyDown event, after storing the key press, do a check to see if all three keys that you expect are down.
It would look something like so:
public void OnKeyDown(EventArgs ...) {
_keyStore.KeyDown(eventArgs.keyPress);
if(_keyStore.AltCtrlKeyPad4IsDown()) { //Abstract this so you can perform multiple checks.
//Do shortcut.
}
}
public void OnKeyUp(EventArgs ...) {
_keyStore.KeyUp(eventArgs.keyPress);
}
Note that this is sudo code and will not compile.
I got a thought experiment with a graphical user interface for writing a command.
There are buttons for saving, writing cursive and so on.....
Now when i press the button save, i want to that the compiler perform Ctrl+S like in every textdocument, or change the marked one to cursive with Ctrl+Shift+K.
I tried to set the variable to the value of the ascii code, but this ainĀ“t worked. Now i need help that the compiler perform the hotkey.
Is there a function in C# or is there another way to realize that.
Actually those hotkeys are implemented functions of the programs you use (e.g. word). The hotkey just executes the same function as the button you can press - the compiler doesn't know the hotkey Ctrl+Shift+K.
Just think about it: If the compiler knew about those hotkeys, it would have to know how to make the selected text in the focused textbox cursive - wouldn't this be kind of strange?
I might misunderstand your question, maybe you just wanted to ask how to type text with a simulated keyboard for the form, because you already implemented the shortcut functionability. In that case you'd have two options:
Call the function called when you press the specified shortcut
Actually simulate keypresses to the form
The second one is done like that:
SendKeys.Send("^S"); //For Ctrl+S - ^ is the Control button
For other special keys with the SendKeys function take a look at this: https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx.
I'm making my own multiple clipboard copy/paste tool which runs in background, and I've finally achieved it, it's working.
this is how it works,
when Capslock it's pressed, if I press CTRL+1 I make a CTRL+C programmatically ( with SendKeys) and save the clipboard on my list with correct position
when capslock it's not pressed, if I press CTRL+1 I make a CTRL+V programmatically ( with send keys) by using the latest data in clipboard on the correct list position.
Now it's fine, but I want to make a little change, I don't want to use capslock, but I want to press another key, like ALT or SHIFT, but if you keep pressing a key which is not the CTRL and then you do a CTRL+C, it does not work.
Anyone have any advice to this dumb thing?
Thanks guy
If you don't mind using WinApi functions, you can use RegisterHotKey and UnregisterHotKey functions.
They allow you to register and unregister global shortcuts of your choice. This way you'll get notified about a shortcut being pressed even if your application is running in background and doesn't have the focus on itself.
You can find more information about both functions on pinvoke here and here. There is even some sample application code so you can see how to use them.
NOTE: Remember to unregister all the shortcuts that you've registered on application exit.
You can use a flag for the select key. When you check to see what keys are pressed then:
something like the following pseudo code should do the trick:
if(select key)
{
this.selectKey = true;
}
else
{
if(ctrl key)
{
//do whatever you would normally do here
}
this.selectKey = false;
}
I am trying to use single letters to navigate through the menu strip. I know that I can use & to show which letter I might click to represent each item.
But how do I actually set up single key shortcuts?
I was looking through the properties panel, but none of them seem to do this.
Oh wait, is that even possible? Or should I use ALT + Key to do this? Regular programs use ALT + Key combinations.
When adding a key shortcut through the & character on a menu item, you can access the menu by typing ALT + Key
You can use code like this:
this.InputBindings.Add(new KeyBinding(ApplicationCommands.Close, new KeyGesture(Key.C)));
This is a example for the application command "close". And it will be triggered by press the key "c". This code you have to write in the constuctor of the window or whatever because it only work on this way.
This is the standard behavior for a menu in Windows. If you show the menu, and your menu looks like:
A
B
-
C
D
And you press D key, the action D will be executed.
If you are using the ALT + key functionality, you need to add & character on the menu item, as #Otiel said.
i want to build a simple program which help you selecting pictures.
if you have lot of picture and you want to choose some of them then you see them 1 by 1 and when you see a picture you would lik to save on other folder on your pcyou just press a button ,lets say f5 and the program copy the phtot from the path you looking at to the destiny folder.
for that program i need to ask how to know if someone pressed f5 out of the form area and how to know in which path the user looking at.(i want to build it for myself atm so if its help i look with microsoft office picture manager)
about the clicking i search a little and get something named global clicking and hooks which i dont understand so much and about identify the path i have no idea .
tyvm for help:)
I'm not sure I follow the rest -- but if you want to capture the keypress event, simply add an event handler for KeyPress and determine if the pressed key is equal to the F5 button by using the Keys constants.
Here is a project on Code Project that does exactly what you need :)
http://www.codeproject.com/KB/cs/globalhook.aspx
The key press event wont work with the following (Link):
TAB
INSERT
DELETE
HOME
END
PAGE UP
PAGE DOWN
F1-F2
ALT
Arrow keys
Note: I think there is a typo on the page and the F1-F2 really should be F1-F12.
When you decide the key for your on key press event for the form area you are talking about it will look like this:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == [keypressvalue])
{
//do your copy logic
}
}
[keypressvalue] will be the code for F5 if you choose to use this. I have found a mix of values for this (i could not get my test keypress event to pick up the F5 event, hence my note above) so i recommend running the event once with a brake point, inspecting the code, then brake and update your code, then test your logic.
Like the rest i'm not really sure what you want your custom logic to do.
Clairifcation: I'm trying to understand your question, so what you want is: When in Microsoft Picture Manager when you press F5 you want the image that is currently being viewed to be moved to a particular directory? Now if you are writing your own picture viewer and move software then i think we can help if it is the above i'm not really sure you can do that.