473,378 Members | 1,142 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to Piggyback a Keypress?

Is there a easy way to resend the KeyPress to the window after
capturing a hotkey?

protected override void WndProc(ref Message m) {
base.WndProc(ref m);
if(m.Msg == WM_HOTKEY) {
if(m.WParam == myHotkeyID) /* Do What I Want to
Do */
Resend the Key Pressed to the application without
triggering the hotkey message again.
}
}
}

it works if I use the C# "SendKeys.Send(...)", and mimics the "ctrl/
shift/alt modifiers" nicely, but the hotkeys are defined by the user,
and converting the "Keys.___" to a string manually is hard at times...
"IE Keys.OemTilda.ToString()" != "`"

Is using PostMessage the easiest? Getting the SYSKEYDOWN, KEYDOWN,
lParam Mimic etc all perfect would take a tiny bit..

Would there be a trick with the DefWndProc/DefWindowProc I could use?

Jun 5 '07 #1
5 6908
Well, why do you want to resend the keypress? What are you trying to
do? If all you want to do is do your processing, then let the application
react normally to the keypress, then have done it already (although in a
different order) by calling the WndProc method on the base implementation
(the base.WndProc line).

If you want to do your processing before passing the key along, then
move the call to base.WndProc after your if statement.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"NvrBst" <nv****@gmail.comwrote in message
news:11**********************@g37g2000prf.googlegr oups.com...
Is there a easy way to resend the KeyPress to the window after
capturing a hotkey?

protected override void WndProc(ref Message m) {
base.WndProc(ref m);
if(m.Msg == WM_HOTKEY) {
if(m.WParam == myHotkeyID) /* Do What I Want to
Do */
Resend the Key Pressed to the application without
triggering the hotkey message again.
}
}
}

it works if I use the C# "SendKeys.Send(...)", and mimics the "ctrl/
shift/alt modifiers" nicely, but the hotkeys are defined by the user,
and converting the "Keys.___" to a string manually is hard at times...
"IE Keys.OemTilda.ToString()" != "`"

Is using PostMessage the easiest? Getting the SYSKEYDOWN, KEYDOWN,
lParam Mimic etc all perfect would take a tiny bit..

Would there be a trick with the DefWndProc/DefWindowProc I could use?
Jun 5 '07 #2
Basically I want to set up a global hotkey for a single (external)
application only. For example, I have my program register a F1
hotkey. Whenever the hotkey is pressed I check to see if the
foreground application is the one I want it to be. If it is I have my
applicaiton react to the F1 key being pressed, if it isn't the
application I want it to be then my program does nothing and I want
the F1 key to be pressed normally like my application wasn't up.

The "base.WndProc(ref m);" doesn't seem to affect anything for the
external programs... Once my program registers the F1 hotkey, no other
application react to the F1 key being pressed (Even with the
"base.WndProc(ref m); line there).

Another options would be unregister all my hotkeys when the
application I want them to affect loses focus, then re-register them
when it gains focus again, but I don't know how to do that other than
setting up a loop that keeps checking forground window which would be
too costly for me.

NB

On Jun 5, 4:26 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Well, why do you want to resend the keypress? What are you trying to
do? If all you want to do is do your processing, then let the application
react normally to the keypress, then have done it already (although in a
different order) by calling the WndProc method on the base implementation
(the base.WndProc line).

If you want to do your processing before passing the key along, then
move the call to base.WndProc after your if statement.

--
- Nicholas Paldino [.NET/C# MVP]
Jun 6 '07 #3
So wait a second, why not just code the individual applications to react
to the key that is pressed?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"NvrBst" <nv****@gmail.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
Basically I want to set up a global hotkey for a single (external)
application only. For example, I have my program register a F1
hotkey. Whenever the hotkey is pressed I check to see if the
foreground application is the one I want it to be. If it is I have my
applicaiton react to the F1 key being pressed, if it isn't the
application I want it to be then my program does nothing and I want
the F1 key to be pressed normally like my application wasn't up.

The "base.WndProc(ref m);" doesn't seem to affect anything for the
external programs... Once my program registers the F1 hotkey, no other
application react to the F1 key being pressed (Even with the
"base.WndProc(ref m); line there).

Another options would be unregister all my hotkeys when the
application I want them to affect loses focus, then re-register them
when it gains focus again, but I don't know how to do that other than
setting up a loop that keeps checking forground window which would be
too costly for me.

NB

On Jun 5, 4:26 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> Well, why do you want to resend the keypress? What are you trying to
do? If all you want to do is do your processing, then let the
application
react normally to the keypress, then have done it already (although in a
different order) by calling the WndProc method on the base implementation
(the base.WndProc line).

If you want to do your processing before passing the key along, then
move the call to base.WndProc after your if statement.

--
- Nicholas Paldino [.NET/C# MVP]

Jun 6 '07 #4
Only the application that registers the hotkeys is mine... Don't have
the source code of the application that should be active when the
hotkeys are being pressed, or the applications that are active when I
do something else quick (IE Internet Explorer).

On Jun 6, 12:16 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
So wait a second, why not just code the individual applications to react
to the key that is pressed?

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
Jun 6 '07 #5
Using the PostMessage Method turned out to be easier than I thought it
was going to be. Heres my code if anyone ever runs into a simular
problem and wants to send a key :)

------------------------------------------------------------------------------------------
[Flags]
public enum mySendKeyFlags : byte {
None = 0x00,
SimReal = 0x01,
SendExtendedKey = 0x02
}
public void SendKey(int key, int[] Modifier, IntPtr
ForGroundWindow, mySendKeyFlags Flags) {
if(Flags & mySendKeyFlags.SimReal !=0) {
byte[] kbState = new byte[255];

AttachThreadInput((uint)AppDomain.GetCurrentThread Id(), (uint)*GET
PROCESS ID OF CURRENT APP*, true);
GetKeyboardState(kbState);
byte[] kbStateMOD = kbState;
for(byte i = 0; i < Modifier.Length; i++)
kbStateMOD[Modifier[i]] = 0x80;
SetKeyboardState(kbStateMOD);

SendKeyHelp(ref key, ref Modifier, ref
ForGroundWindow, ref Flags);

SetKeyboardState(kbState);

AttachThreadInput((uint)AppDomain.GetCurrentThread Id(), (uint)*GET
PROCESS ID OF CURRENT APP*, false);
} else SendDAKeyHelp(ref key, ref Modifier, ref
ForGroundWindow, ref Flags);
}
private void SendKeyHelp(ref int key, ref int[] Modifier, ref
IntPtr ForGroundWindow, ref mySendKeyFlags Flags) {
if(Modifier != null) {
for(byte i = 0; i < Modifier.Length; i++) {
if(Modifier[i] == (int)Keys.Alt)
PostMessage(Params.hWnd, WM_SYSKEYDOWN, Modifier[i],
(int)MAKELONG(0x1, (ushort)MapVirtualKey((uint)Modifier[i], 0)));
else PostMessage(Params.hWnd, WM_KEYDOWN,
Modifier[i], (int)MAKELONG(0x1,
(ushort)MapVirtualKey((uint)Modifier[i], 0)));
}
}
if(Flags & mySendKeyFlags.SendExtendedKey != 0)
PostMessage(Params.hWnd, WM_KEYDOWN, key, (int)MAKELONG(0x1, (ushort)
(MapVirtualKey((uint)key, 0) | 0x0100)));
else PostMessage(Params.hWnd, WM_KEYDOWN, key,
(int)MAKELONG(0x1, (ushort)MapVirtualKey((uint)key, 0)));

if(Flags & mySendKeyFlags.SimReal != 0) Thread.Sleep(10);

if(Flags & mySendKeyFlags.SendExtendedKey != 0)
PostMessage(Params.hWnd, WM_KEYUP, key, (int)MAKELONG(0x1, (ushort)
(MapVirtualKey((uint)key, 0) | 0xC100)));
else PostMessage(Params.hWnd, WM_KEYUP, key,
(int)MAKELONG(0x1, (ushort)(MapVirtualKey((uint)key, 0) | 0xC000)));
if(Modifier != null) {
for(byte i = 0; i < Modifier.Length; i++) {
if(Modifier[i] == (int)Keys.Alt)
PostMessage(Params.hWnd, WM_SYSKEYUP, Modifier[i], (int)MAKELONG(0x1,
(ushort)(MapVirtualKey((uint)key, 0) | 0xC000)));
else PostMessage(Params.hWnd, WM_KEYUP,
Modifier[i], (int)MAKELONG(0x1, (ushort)
(MapVirtualKey((uint)Modifier[i], 0) | 0xC000)));
}
}
}
------------------------------------------------------------------------------------------

Basically the Key is the Virtual Key code (given in the WM_HOTKEY
message alonge with the Modifiers used). You have to convert the
modifers to an int[] of VKcodes. Have to manual give the extended key
flag if the hotkey is an extended one. Use SimReal if you have
modifiers (ctrl/alt/shift/etc) but you have to get the process id of
the current app (Most my hotkeys don't have modifiers which is why I
have a flag I set only when I have to use it). If anyone knows an
easy way to get the active windows process id (maybe a winapi?) it
would be nice (mine just goes though all processes and checks their
MainWindowHandle against the GetForgroundWindow() that I have).

I was forced to add a 10ms delay in the middle for the SimReal case
(for the application to produce its WM_CHAR message after the
WM_KEYDOWN message is sent... I don't know how to know when the
external message loop produced it.

I'm still in the process of tested/making it the way I want it. If
anyone knows a quick solutions to the processID / WM_CHAR message
problems it would be a help.

Jun 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Darryn Ross | last post by:
Hi, I am trying to catch the KeyPress event on my datagrid but it isn't working... i have also tried registering the handler with the event like this... dgGLBatch.KeyPress += new...
2
by: rege | last post by:
Hi, I have two applications developed in C# say A and B. When user presses key "L" with applicaition A active , it causes a keypress event which causes application B to run. Application A then...
1
by: Rene | last post by:
Hi, I am running is some problems with the KeyPreview and KeyPress events. The KeyPress event is only triggered when there this an focusable control on the form. When all controls are disabled...
4
by: Tom | last post by:
I have a VB.NET user control that I wrote - this control has three or four other controls on it (textbox, combobox, datetime picker, etc). Now, whenever the control is on a form and the user enters...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
3
by: Fia | last post by:
Hi In Visual Basic 6 I could call keypress with an integer of a choosen key. For example I could call a textbox's keypress with the number 13 (for the enter key). But now in Visual Basic .Net I...
5
by: Henry Jones | last post by:
I am new to C# and wanted to capture the KeyPress for a textbox. I created some code as follows: private void textBox3_KeyPress(object sender, System.EventArgs e) { this.textBox2.Text =...
2
by: Tony Johansson | last post by:
Hello! I have created a Control that consist of a label and a textbox.I have called this class ctlLabelTextbox. public partial class ctlLabelTextbox : UserControl { .... } The class that I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.