I've created custom button derived from a normal .Net button and have added the following property to add a short cut key combination:
public Keys ShortCutKey { get; set; }
I want this combination to fire the click event of the button but have no idea how to implement this when the button is placed on a form. I know the standard way of doing a button shortcut is to use the & before the short cut character but I need to use a key combination.
Any ideas?
Many Thanks
Override the form's ProcessCmdKey() method to detect shortcut keystrokes. Like this:
private bool findShortCut(Control.ControlCollection ctls, Keys keydata) {
foreach (Control ctl in ctls) {
var btn = ctl as MyButton;
if (btn != null && btn.ShortCutKey == keydata) {
btn.PerformClick();
return true;
}
if (findShortCut(ctl.Controls, keydata)) return true;
}
return false;
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (findShortCut(this.Controls, keyData)) return true;
return base.ProcessCmdKey(ref msg, keyData);
}
Where MyButton is assumed to be your custom button control class.
I'm assuming you are using WinForms, given that the ampersand character is used in WinForms control captions to denote the shortcut character. If that is the case, then you can use the Button.PerformClick() method on a WinForms Button in order to fire the Click event manually.
If this is not the case and you are, in fact, using WPF; then take a look at the link Dmitry has posted in his comment for WPF Input Bindings.
Related
I'm looking for a best way to implement common Windows keyboard shortcuts (for example Ctrl+F, Ctrl+N) in my Windows Forms application in C#.
The application has a main form which hosts many child forms (one at a time). When a user hits Ctrl+F, I'd like to show a custom search form. The search form would depend on the current open child form in the application.
I was thinking of using something like this in the ChildForm_KeyDown event:
if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)
// Show search form
But this doesn't work. The event doesn't even fire when you press a key. What is the solution?
You probably forgot to set the form's KeyPreview property to True. Overriding the ProcessCmdKey() method is the generic solution:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.F)) {
MessageBox.Show("What the Ctrl+F?");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
On your Main form
Set KeyPreview to True
Add KeyDown event handler with the following code
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.N)
{
SearchForm searchForm = new SearchForm();
searchForm.Show();
}
}
The best way is to use menu mnemonics, i.e. to have menu entries in your main form that get assigned the keyboard shortcut you want. Then everything else is handled internally and all you have to do is to implement the appropriate action that gets executed in the Click event handler of that menu entry.
You can even try this example:
public class MDIParent : System.Windows.Forms.Form
{
public bool NextTab()
{
// some code
}
public bool PreviousTab()
{
// some code
}
protected override bool ProcessCmdKey(ref Message message, Keys keys)
{
switch (keys)
{
case Keys.Control | Keys.Tab:
{
NextTab();
return true;
}
case Keys.Control | Keys.Shift | Keys.Tab:
{
PreviousTab();
return true;
}
}
return base.ProcessCmdKey(ref message, keys);
}
}
public class mySecondForm : System.Windows.Forms.Form
{
// some code...
}
If you have a menu then changing ShortcutKeys property of the ToolStripMenuItem should do the trick.
If not, you could create one and set its visible property to false.
From the main Form, you have to:
Be sure you set KeyPreview to true( TRUE by default)
Add MainForm_KeyDown(..) - by which you can set here any shortcuts you want.
Additionally,I have found this on google and I wanted to share this to those who are still searching for answers. (for global)
I think you have to be using user32.dll
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
/* Note that the three lines below are not needed if you only want to register one hotkey.
* The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed.
int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed.
MessageBox.Show("Hotkey has been pressed!");
// do something
}
}
Further read this http://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/
Hans's answer could be made a little easier for someone new to this, so here is my version.
You do not need to fool with KeyPreview, leave it set to false. To use the code below, just paste it below your form1_load and run with F5 to see it work:
protected override void OnKeyPress(KeyPressEventArgs ex)
{
string xo = ex.KeyChar.ToString();
if (xo == "q") //You pressed "q" key on the keyboard
{
Form2 f2 = new Form2();
f2.Show();
}
}
In WinForm, we can always get the Control Key status by:
bool IsCtrlPressed = (Control.ModifierKeys & Keys.Control) != 0;
The VB.NET version of Hans' answer.
(There's a ProcessCmdKey function template in Visual Studio.)
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If (keyData = (Keys.Control Or Keys.F)) Then
' call your sub here, like
SearchDialog()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class
I have a form with a text box.
myTB.KeyDown += new KeyEventHandler(this.myTB_KeyDown);
In this event I can get Keys.Apps to open the context menu.
Now another shortcut for context menu is Shift + F10.
Is it possible to capture that too inside the KeyDown event?
Any logic that I can implement to capture those keys?
So far what I see is, when the Shift key is pressed, that time itself the KeyDown event get's called and no way checking for both Shift and F10 together!
If I understand your question correctly, and you want to disable default Shift+F10 menu and handle those combination yourself, it's enough to handle KeyDown event and detect the combination and the set e.Handled=true;:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode== Keys.F10 && e.Shift==true)
{
//Shift + F10 pressed, do what you need.
e.Handled = true;
}
}
You can also disable the default context menu that will be shown, by setting ShortcutsEnabled property of your TextBox to false.
Here is a pretty safe way to check for shift + F10
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.F10 | Keys.Shift))
{
if (txtBox1.Focused)
{
txtBox1.Text = "Captured!";
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
Description:
This will capture every keystroke. From there is will check if the keystroke is Shift+F10, the single pipe acts like &&. After that it does a simple check to see if the textbox you are planning on having the event happen is focuses, or active control. If it fails either of those checks it sends the information of the keystrokes to return base.ProcessCmdKey(ref msg, keyData); which will give normal functionality to the keystroke without any overridden checks or whatever you may want to do with the captured keystrokes.
I'm using Winforms.
I've a screen approx. 10 fields. and a Update button.
But I don't want to use neither show on screen a button (btnUpdate).
I just want to show the the fields, they can change some values and by pressing the enter it should execute a function in code behind.
I googled and find some solutions like KeyPress on TextBox or whatever, but I don't want to link this to a TextBox. Then I found form.Acceptbutton = btnUpdate... but then I have to use a button on my designer.
so how can I make a situtation by not USING a Button control to do an update (in other words executing function in code-behind by pressing the Enter Key).
Try overriding the ProcessCmdKey
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Return)
{
//Raise Update Event
return true;
}
else if (keyData == Keys.Escape)
{
//Raise Cancel Event
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Is there anyway to check if a menu item event is coming from a click in the menu or from a short cut key being pressed?
I've tried adding event handlers to the key press and key down events, however these events aren't being "fired" when it's a shortcut key that's pressed (They do work as expected when it's not a shortcut key). I couldn't find anything in the sender object that was different between the menu click or shortcut click.
You can catch all key combinations by overriding ProcessCmdKey:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.F))
{
Console.WriteLine("My code ran from shortcut");
myFunction();
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void ToolStripMenuItem_click(object sender ...)
{
Console.WriteLine("My code ran from menu item");
myFunction();
}
void myFunction()
{
//your functionality here
}
Well to get help, you should post what you have tried. (Your source)
You can use a enum for this:
enum Sender
{
Shortcut,
Menu
}
void MenuEvent(Sender sender)
{
if (sender == Sender.Shortcut)
{
}
else
{
}
}
//if you click from the menu
void btnMenuClick()
{
MenuEvent(Sender.Menu);
}
//if you use shortcut
void OnShortcutEvent()
{
MenuEvent(Sender.Shortcut);
}
Edit: I guess my answer was to vague so I edited the code. I hope its more clear now, but I must say the OP should give more details as well, such as posting some code.
I see a single solution to this problem - override the ToolStripMenuItem's ProcessCmdKey method which is raised when a shortcut is processed. In this case, you can determine when a click was caused by a shortcut. Obviously, you need to use your own class of the ToolstripMenuItem instead of the standard one.
Handle the MouseDown event to process your mouse-click.
menuItem.MouseDown += new MouseEventHandler(Process_Mouse_Click_Handler);
Handle the Click event to process your shortcut.
menuItem.Click+= new EventHandler(Process_Shortcut_Handler);
Is it somehow possible to prevent end users from copying text from RichTextBox in a C# application? Maybe something like adding a transparent panel above the RichTextBox?
http://www.daniweb.com/software-development/csharp/threads/345506
Re: Disable copy and paste control for Richtextbox control Two steps
to be followed to disable the Copy-Paste feature in a textbox,
1) To stop right click copy/paste, disable the default menu and
associate the textbox with an empty context menu that has no menu
items.
2) To stop the shortcut keys you'll need to override the ProcessCmdKey
method:
C# Syntax (Toggle Plain Text)
private const Keys CopyKey = Keys.Control | Keys.C;
private const Keys PasteKey = Keys.Control | Keys.V;
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if((keyData == CopyKey) || (keyData == PasteKey)){
return true;
} else {
return base.ProcessCmdKey(ref msg, keyData);
}
}
You need to subclass the rich edit box control, override the WM_COPY message, and do nothing when you receive it. (Do not delegate to DefWindowProc.)
You will also have to do the same for WM_CUT.
And still anyone will be able to get the text using some utility like Spy++.
One way to doing it is make access to clipbord object and than you can manitpulate content of clipbord.
method for clearing clipbord is : Clipboard.Clear Method
You could do this (I think):
//Stick this in the KeyDown event of your RichtextBox
if (e.Control && e.KeyCode == Keys.C) {
e.SuppressKeyPress = true;
}