Catching the ALT key pressed?

  • Thread starter Thread starter =?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno
  • Start date Start date
?

=?ISO-8859-1?Q?=22Andr=E9s_G=2E_Aragoneses_=5B_kno

Hello. I am trying to catch the ALT key (but not as a modifier, but as a
single key clicked) on a Console application, but neither Read(),
ReadLine() nor ReadKey() catch it, how can I do it using the
System.Console class?

Thanks in advance.

Regards.

Andrés [ knocte ]

--
 
This isn't possible in .NET as far as I'm aware, you will need to hook into
the Windows API to handle the keypress events at a far lower level as .NET
treats the ALT key as a modifier only.

There is an article about hooking in but for a different reason here (but it
should be adaptable):
http://www.codeproject.com/useritems/CSLLKeyboard.asp

Ciaran O'Donnell
 
It can be obtained by overriding the ProcessCommandKey method. Here's an
example (note that I created a separate class to hold WinAPI constants):

/// <summary>
/// KeyDown Event handler which handles control keys as well as regular
/// </summary>
/// <param name="msg"><c>System.Windows.Forms.Message</c></param>
/// <param name="keyData">Key Data</param>
/// <returns>True if handled; false if not</returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!toolPageNumber.Focused && !toolDocumentName.Focused && msg.Msg ==
WinUserApi.WM_KEYDOWN)
{
switch ((int)msg.WParam)
{
case WinUserApi.VK_UP:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (vertScroll.Visible)
ScrollVertical(Math.Max(vertScroll.Value - vertScroll.SmallChange,
0));
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(0, -1);
}
break;
case WinUserApi.VK_DOWN:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (vertScroll.Visible)
ScrollVertical(Math.Min(vertScroll.Value + vertScroll.SmallChange,
_MaxNegativeVerticalSpace));
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(0, 1);
}
break;
case WinUserApi.VK_LEFT:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (horizScroll.Visible)
ScrollHorizontal(horizScroll.Value - horizScroll.SmallChange);
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(-1, 0);
}
break;
case WinUserApi.VK_RIGHT:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (horizScroll.Visible)
ScrollHorizontal(horizScroll.Value + horizScroll.SmallChange);
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(1, 0);
}
break;
case WinUserApi.VK_PRIOR:
if (!InputEnabled) return true;
if (pagePanel.SelectedElements.Count == 0)
{
if (vertScroll.Visible)
ScrollVertical(Math.Max(vertScroll.Value - vertScroll.LargeChange,
0));
return true;
}
else
pagePanel.MoveCurrentPrintElementBy(0, -20);
break;
case WinUserApi.VK_NEXT:
if (!InputEnabled) return true;
if (pagePanel.SelectedElements.Count == 0)
{
if (vertScroll.Visible)
ScrollVertical(Math.Min(vertScroll.Value + vertScroll.LargeChange,
_MaxNegativeVerticalSpace));
return true;
}
else
pagePanel.MoveCurrentPrintElementBy(0, 20);
break;
case WinUserApi.VK_DELETE:
if (!InputEnabled) return true;
pagePanel.DeleteCurrentElement();
return true;
case WinUserApi.VK_CONTROL:
pagePanel.HandleElementMouseEvents = false;
break;
case WinUserApi.VK_TAB:
pagePanel.HandleElementMouseEvents = false;
if (!InputEnabled || pagePanel.HandleElementMouseEvents) return true;
if (pagePanel.PrintElements.HighestZIndex == -1) return true;
int i = pagePanel.PrintElements.IndexOfZ(_CurrentZIndex);
if (i == -1) i = 0;
if (pagePanel.ShiftOn) i = (i == 0 ?
pagePanel.PrintElements.HighestZIndex : i - 1);
else i = (i == pagePanel.PrintElements.HighestZIndex ? 0 : i + 1);
if (i == -1) return false;
pagePanel.CurrentPrintElement = pagePanel.PrintElements;
if (pagePanel.CurrentPrintElement != null)
{
pagePanel.CurrentPrintElement.ShowToolTip();
ShowStatusMessage("PrintElements[" + i.ToString() +
"] (" + pagePanel.PrintElements.ToString() +
") " + pagePanel.CurrentPrintElement.Location.ToString());
}
return true;
case WinUserApi.VK_SHIFT:
pagePanel.ShiftOn = true;
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}

Ciaran O''Donnell said:
This isn't possible in .NET as far as I'm aware, you will need to hook
into
the Windows API to handle the keypress events at a far lower level as .NET
treats the ALT key as a modifier only.

There is an article about hooking in but for a different reason here (but
it
should be adaptable):
http://www.codeproject.com/useritems/CSLLKeyboard.asp

Ciaran O'Donnell

"Andrés G. Aragoneses [ knocte ]" said:
Hello. I am trying to catch the ALT key (but not as a modifier, but as a
single key clicked) on a Console application, but neither Read(),
ReadLine() nor ReadKey() catch it, how can I do it using the
System.Console class?

Thanks in advance.

Regards.

Andrés [ knocte ]
 
Kevin Spencer escribió:
It can be obtained by overriding the ProcessCommandKey method. Here's an
example (note that I created a separate class to hold WinAPI constants):

Thanks for the info, but this code snippet seems only valid for a
WinForms application (instead of a Console application) and it seems
very tied to Win32 API (so I suppose for Linux with Mono it won't work
and it would be more code needed), am I right?

Regards,

Andrés [ knocte ]

--
 
Kevin,
Can it be used to catch teh Send and End Keys also.
I am trying to capture the End(Red) and Send(Green) keys for my call
application .How can i do that in C#

Kevin Spencer said:
It can be obtained by overriding the ProcessCommandKey method. Here's an
example (note that I created a separate class to hold WinAPI constants):

/// <summary>
/// KeyDown Event handler which handles control keys as well as regular
/// </summary>
/// <param name="msg"><c>System.Windows.Forms.Message</c></param>
/// <param name="keyData">Key Data</param>
/// <returns>True if handled; false if not</returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (!toolPageNumber.Focused && !toolDocumentName.Focused && msg.Msg ==
WinUserApi.WM_KEYDOWN)
{
switch ((int)msg.WParam)
{
case WinUserApi.VK_UP:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (vertScroll.Visible)
ScrollVertical(Math.Max(vertScroll.Value - vertScroll.SmallChange,
0));
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(0, -1);
}
break;
case WinUserApi.VK_DOWN:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (vertScroll.Visible)
ScrollVertical(Math.Min(vertScroll.Value + vertScroll.SmallChange,
_MaxNegativeVerticalSpace));
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(0, 1);
}
break;
case WinUserApi.VK_LEFT:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (horizScroll.Visible)
ScrollHorizontal(horizScroll.Value - horizScroll.SmallChange);
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(-1, 0);
}
break;
case WinUserApi.VK_RIGHT:
if (pagePanel.SelectedElements.Count == 0)
{
if (!InputEnabled) return true;
if (horizScroll.Visible)
ScrollHorizontal(horizScroll.Value + horizScroll.SmallChange);
return true;
}
else
{
if (!InputEnabled && pagePanel.CurrentPage == 0) return true;
pagePanel.MoveCurrentPrintElementBy(1, 0);
}
break;
case WinUserApi.VK_PRIOR:
if (!InputEnabled) return true;
if (pagePanel.SelectedElements.Count == 0)
{
if (vertScroll.Visible)
ScrollVertical(Math.Max(vertScroll.Value - vertScroll.LargeChange,
0));
return true;
}
else
pagePanel.MoveCurrentPrintElementBy(0, -20);
break;
case WinUserApi.VK_NEXT:
if (!InputEnabled) return true;
if (pagePanel.SelectedElements.Count == 0)
{
if (vertScroll.Visible)
ScrollVertical(Math.Min(vertScroll.Value + vertScroll.LargeChange,
_MaxNegativeVerticalSpace));
return true;
}
else
pagePanel.MoveCurrentPrintElementBy(0, 20);
break;
case WinUserApi.VK_DELETE:
if (!InputEnabled) return true;
pagePanel.DeleteCurrentElement();
return true;
case WinUserApi.VK_CONTROL:
pagePanel.HandleElementMouseEvents = false;
break;
case WinUserApi.VK_TAB:
pagePanel.HandleElementMouseEvents = false;
if (!InputEnabled || pagePanel.HandleElementMouseEvents) return true;
if (pagePanel.PrintElements.HighestZIndex == -1) return true;
int i = pagePanel.PrintElements.IndexOfZ(_CurrentZIndex);
if (i == -1) i = 0;
if (pagePanel.ShiftOn) i = (i == 0 ?
pagePanel.PrintElements.HighestZIndex : i - 1);
else i = (i == pagePanel.PrintElements.HighestZIndex ? 0 : i + 1);
if (i == -1) return false;
pagePanel.CurrentPrintElement = pagePanel.PrintElements;
if (pagePanel.CurrentPrintElement != null)
{
pagePanel.CurrentPrintElement.ShowToolTip();
ShowStatusMessage("PrintElements[" + i.ToString() +
"] (" + pagePanel.PrintElements.ToString() +
") " + pagePanel.CurrentPrintElement.Location.ToString());
}
return true;
case WinUserApi.VK_SHIFT:
pagePanel.ShiftOn = true;
break;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}

Ciaran O''Donnell said:
This isn't possible in .NET as far as I'm aware, you will need to hook
into
the Windows API to handle the keypress events at a far lower level as .NET
treats the ALT key as a modifier only.

There is an article about hooking in but for a different reason here (but
it
should be adaptable):
http://www.codeproject.com/useritems/CSLLKeyboard.asp

Ciaran O'Donnell

"Andrés G. Aragoneses [ knocte ]" said:
Hello. I am trying to catch the ALT key (but not as a modifier, but as a
single key clicked) on a Console application, but neither Read(),
ReadLine() nor ReadKey() catch it, how can I do it using the
System.Console class?

Thanks in advance.

Regards.

Andrés [ knocte ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top