is there any other way besides

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.B)
            {
                MessageBox.Show("Enter pressed!");
                String fileName = @"C:/Windows/Media/tada.wav";
                PlaySound(fileName, 0, 1);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

that to capture key presses i want to beable to capture more than one. for example in my app if you press O the cd tray opens. i want to be aable to press C and it close but for some reason when i try to put that code in there again it gives me an error

any on ideas on code that ould let me do this would be helpful

thankyou

tayspen

Dani AI

Generated

Short version: 's switch-in-override is a perfectly valid way to handle multiple keys. Another simple, often cleaner approach is to let the form see keystrokes first (KeyPreview) and use the form-level KeyDown/KeyPress events for your shortcuts. If a control completely swallows keys (some third-party controls, DataGridView editors, browser control, etc.), override ProcessCmdKey instead — it runs earlier in message preprocessing and is meant for command/accelerator keys. (learn.microsoft.com)

Set the form to preview keys and handle KeyDown. KeyDown gives you physical-key info (Keys.O, Keys.C) and lets you check modifiers; KeyPress gives character-level info (useful if you care about 'c' versus 'C'). Example pattern (wire events in designer or constructor):

this.KeyPreview = true;

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.O) { OpenCd(); e.SuppressKeyPress = true; }
    else if (e.KeyCode == Keys.C) { CloseCd(); e.SuppressKeyPress = true; }
}

Use e.SuppressKeyPress/e.Handled or return true from ProcessCmdKey to stop the focused control from also receiving the keystroke. Use e.Modifiers or e.KeyData to detect combos like Ctrl+S. (learn.microsoft.com)

To open/close the CD tray you can call the Windows MCI API (winmm.dll). Typical P/Invoke and commands:

[DllImport("winmm.dll")]
static extern int mciSendString(string command, StringBuilder returnValue, int returnLength, IntPtr winHandle);

mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero);

Check the return value and use mciGetErrorString for diagnostics if it fails. (pinvoke.net)

Troubleshooting: verify the KeyDown handler is wired, ensure KeyPreview = true when using form-level events, and if a control still swallows keys switch to ProcessCmdKey and return true when handled. This keeps the UI responsive and prevents duplicate handling. (referencesource.microsoft.com)

Recommended Answers

All 2 Replies

Hope this helps

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{ 
			bool result = false;
			switch (keyData)
			{
				case(Keys.B):
					MessageBox.Show("Enter pressed!");
					String fileName = @"C:/Windows/Media/tada.wav";
					PlaySound(fileName, 0, 1);
					result = true;
					break;
				case(Keys.O):
					//code to open cd tray here
					result = true;
					break;
				case(Keys.C):
					//code to close cd tray here
					result = true;
					break;
			}
			return result;		}

yup exactley what i was looking for thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.