For example, say I wanted to switch Ctrl+C and Ctrl+V so that Ctrl+C will paste and Ctrl+V will copy. I will need to first override Ctrl+Ccopying and then make Ctrl+C paste. I will have to do the same with Ctrl + V as well, except I will need to override the paste and make it copy.
How should I go about doing this? Is this possible in a UWP app using C#? Whenever I look up something along the lines of "remapping keyboard shortcuts in c#", it shows me results for remapping keyboard shortcuts in Visual Studio, not for in my app.
>How do I remap keyboard shortcuts in UWP C# app?
It is a little complicated but you could do it in your app. You need to handle the keyboard input event - CoreWindow.KeyDown Event first. In the input event, check both if the control key is pressed and if the C key is pressed. When both of them are pressed, then you could do your own logic in the event handler.
Here is a sample code about how to handle the keydown event.
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
if (ctrl.HasFlag(CoreVirtualKeyStates.Down) && args.VirtualKey == VirtualKey.V)
{
//clean the data in the clipboard.
Windows.ApplicationModel.DataTransfer.Clipboard.Clear();
// do your stuff
Debug.WriteLine("Ctr+V");
return;
} else if(ctrl.HasFlag(CoreVirtualKeyStates.Down) && args.VirtualKey == VirtualKey.C)
{
//clean the data in the clipboard.
Windows.ApplicationModel.DataTransfer.Clipboard.Clear();
// do your stuff
Debug.WriteLine("Ctr+C");
return;
}
}
Related
I want to trigger Windows hotkeys using my c# application. For example, if I selected copy button in my Application, i want to trigger the Ctrl - C hotkey. If I selected the run button in my application, I want to trigger the Win - R hotkey.
How can I do that?
Thank you.
You can use SendKeys.Send method
For example in your button click event, to trigger CTRL + C combination you can use this
SendKeys.Send("^c") // CTRL + C
Note: By the way I wouldn't suggest you to do it in button click event.Probably you are trying to copy some text from your textbox.But when you click your button textbox is losing it's focus and selected text is disappear.So key is sending correctly but you can't copy anything.
"If I selected the run button in my application, I want to trigger the Win - R hotkey."
Run Dialog
In JScript, it's a lot simpler do display the Run dialog box. It is still possible in C#, though. You need a reference to Shell32:
Then add using Shell32; in your code-behind.
In your button's click event, you can do this:
private void runBtn_Click(object sender, EventArgs e)
{
Shell shell = new Shell();
IShellDispatch sd = (IShellDispatch)shell;
sd.FileRun();
}
And you should see somethin' like this:
Simulating Ctrl-C
"...if I selected copy button in my Application, i want to trigger the Ctrl - C hotkey."
Selman22 mentioned the textbox will lose focus if you click another button. Here's the way around it:
private void copyBtn_Click(object sender, EventArgs e)
{
textBox1.Focus(); // <---- here
SendKeys.Send("^c");
}
I have C# app that monitors keystrokes via KeyDown and KeyPress events. Specifically, it watches for a VolumeMute keystroke (from a handheld device) to do some special processing. It works fine with one problem: Windows seems to intercept the VolumeMute keystroke and mutes the system volume (which I don't want to happen). I think Windows intercepts the keystroke before it is processed by my app because even when I signal the keystroke was handled (e.Handled = true), it mutes the system volume anyway. BTW, the same code works perfectly for other keystrokes I'm catching (ex Backspace, ect).
Is there a way to stop Windows from doing this volume mute?
System: WinXP SP3, .Net 4 Client Profile, Windows Forms app
Code snips
bool keyHandled = false;
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (keyHandled)
{
e.Handled = true;
}
}
// =====================================
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
keyHandled = false;
switch (e.KeyCode)
{
case Keys.VolumeMute:
DoSpecialProcessing();
keyHandled = true;
break;
default:
break;
}
e.Handled = keyHandled;
}
The button acts like a toggle, it will mute on the first press and "un-mute" on the next press. All you need is to simulate VolumeMute press again.
This answer explains how to do it.
Note: If you call this method from inside KeyDown handler then you probably need to use BeginInvoke or PostMessage to post a message to a message queue and return immediately in order to avoid race conditions.
Ok. I ended up using AutoHotKey to remap the VolumeMute to another key. Importantly, Windows never sees the VolumeMute keystroke before it gets remapped and doesn't mute the sound. So I can just start this AutoHotKey remap script when I need it. Beats messing with the Windows Registry. Thanks for the suggestions.
This is probably a simple question but i've been unable to find a quick answer.
I have a WPF application which has a Windows Forms Control hosting a GeckoFX component (doesn't really matter).
What i want to do is capture key down events inside the Windows Forms Control and grab focus of a WPF control for some particular key combination.
And what if i want to capture events from the entire WPF application window (even inside the Windows Forms Control)?
I tried handling the KeyDown event and PreviewKeyDown event but to no avail.
What i want to know is if this is possible and how this should be done. I can post some code if required.
The KeyDown even on the Form should work for non special keys (like arrow keys). Use PreviewKeyDown to capture those, or use this solution.
For GeckoWebBrowser specifically, I had to use PreviewKeyDown. Also, I added a line so it doesn't break in design mode:
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
if (DesignMode) return;
if (!e.IsInputKey && e.Control && e.KeyCode == Keys.S) {
DoStuff();
return;
}
}
I am using the below code to capture the ctrl + alt + Q hot keys, which works perfectly.
But, i want to use this in a background application. Since my application does not have any forms, i want to use the same code inside a class file.
i am confuse, because i cannot write a event handler [keypressed] in class file.
Instead i want to use the keypress in thread.
Please help.
public DialogResult Result;
KeyboardHook hook = new KeyboardHook();
public Form1()
{
InitializeComponent();
// register the event that is fired after the key press.
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
// register the control + alt + F12 combination as hot key.
hook.RegisterHotKey((ModifierKeys)2 | (ModifierKeys)1, Keys.Q);
}
void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
Result = MessageBox.Show("Are you sure, you want to log off?","Log off"
,MessageBoxButtons.YesNo
,MessageBoxIcon.Warning);
if (Result == DialogResult.Yes)
{
}
else
{
}
}
If you want to capture a global hotkey without a form, I'm afraid you cannot.
The reason is that global hotkeys are sent to a window handle (and processed in wndProc, aka. the message pump)
So basically the way Windows works, you cannot use global hotkeys without a form to received them.
I'm not entirely certain this is what you want to do however. But on the other there won't be any local hotkeys without a form either, so I cannot see what else it might be.
You may want to further clarify your question a bit (no offense)
The only keyboard hook supported for .NET managed code is a low-level keyboard hook (WH_KEYBOARD_LL).
See Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C#
I have the above code working in my application at the moment so that when you swipe your card you will get a list of all the keystrokes. The problem is for typing delimiter characters such as "%" and ";" it will send me Alt+Numpad+? WPF Key objects corresponding to these symbols.
My question: Is there some way to make this behave more high-level, that is, to capture a string generated from all keyboard commands?
Cheers!
Not sure what's going on, but getting a character like % out of a keyboard hook is very untrivial. The hook only notifies you of virtual keys. But % is a typing key, produced by pressing Shift + 5 on my keyboard (a US layout). Windows normally produces these characters by processing the WM_KEYDOWN/UP messages, generating a WM_CHAR message for the typing key. That's not happening in your case. The low-level Windows function that does this is ToUnicodeEx().
I would guess if you are swiping the card, there's an input somewhere on the wpf form, like a textbox for example? Then I would be inclined to add an event, perhaps a KeyUp Event handler, (The keyboard wedge card scanner does send an end-of-processing signal such as ENTER to indicate the swipe was successful yes?), In the KeyUp Event Handler, build up a string using StringBuilder, and when the end-of-processing signal such as ENTER is caught, you can then remove the "%" and ";" from the StringBuilder instance and do whatever you have to do with it.
It might be easier to use a state system, when the KeyUp event handler receives a "%", then enter another state where the end expected state would be a ";"
static bool StartState = false;
StringBuilder sbInput = new StringBuilder();
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (!StartState){
if (e.KeyCode == Keys.D5) StartState = true;
sbInput.Append((char)e.KeyValue);
}else{
if (e.KeyCode == Keys.OemSemicolon){
StartState = false;
// sbInput will contain the data from the scanner,
// copy it somewhere else and reset sbInput
// sbInput.Remove(0, sbInput.Length);
}
sbInput.Append((char)e.KeyValue);
}
e.Handled = true;
}
Hope this helps,
Best regards,
Tom.