I want to create a shortcut that will perform a task when the letter "Ç" is pressed in my application. But there is no defined "Ç" key. How can I do this? Example:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Alt | Keys.D1:
// Number 1
return true;
case Keys.Alt | Keys.D2:
// Number 2
return true;
case Keys.Ç:
// There is no defined "Ç" (c-cedilla) key.
return true;
default:
return base.ProcessCmdKey(ref msg, keyData);
}
return true;
}
As I understand from Hans Passant's comment, the key assignment changes according to the keyboard combination. So I came up with this solution. Firstly specify the keyboard, then assign shortcut the appropriate OEM key. I know, it's very casuistic and software logic doesn't like casuistic things. But it's better than no solution. The best way I think, assign a shortcut to a standard letter, for example C
.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
switch (keyData)
{
case Keys.Alt | Keys.D1:
// Number 2
case Keys.Alt | Keys.D2:
// Number 1
return true;
// For Turkish Q
default:
{
// For Turkish Q
if (GetLayoutCode() == "0000041F" && keyData is Keys.Oem5)
{
// C-cedilla key.
return true;
}
// For Spanish
if (GetLayoutCode() == "0000040A" && keyData is Keys.Oem2)
{
// C-cedilla key.
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}
private const int KL_NAMELENGTH = 9;
[DllImport("user32.dll")]
private static extern long GetKeyboardLayoutName(StringBuilder pwszKLID);
//each keyboard layout is defined in Windows as a hex code
public static string GetLayoutCode()
{
var name = new StringBuilder(KL_NAMELENGTH);
GetKeyboardLayoutName(name);
return name.ToString();
}
See this topic about keyboard detection.