Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with capturing the arrow keys (when pressed)

Status
Not open for further replies.

Faip

Programmer
Jul 17, 2005
8
0
0
US
Hi,

I'll try to keep this short:

1) Have Comp Sci degree (haven't programmed in about 10 years)
2) Somewhat familiar with C and C++ (currently trying to relearn it)
3) Trying to help a buddy with a project and I am stuck

Here is the problem:

I need a window console that will capture the arrow keys and perform certain functions when captured. One of those funtions being returning the HEX value of the key that was pressed and another being the highlighting/shading of a button on the windows form that corresponds to the key that was pressed. I have spent nearly two days on the internet researching this issue and have found alot of things. Most of these things did not apply to Visual C++ and the others were at a programming level well above my own..for now anyway. Another problem that I am facing is that I am very unfamiliar with creating windows in C++. When I was at school, we didn't do any of that stuff. Anyway, I have rambled on enough. Can anyone help?????

Thanks,

Faip
 
Do you have Visual C++? Otherwise making windows is a big pain in the butt.
 
I am currently using Microsoft Visual C++ 2005 Express Edition Beta 2. I downloaded the trial version that expires in a few days. I plan on getting Microsoft Visual C++ 6. Back when I first learned to program in college, all we did was use a text editor and used a command line compiler. I am trying to wrap my mind around all the windows stuff. With my current non programming job, I haven't had a whole lot of time to relearn C++. I am enjoying trying to understand it all again. Anyway, any ideas on my original problem?

Thanks.
 
Well, if you have Visual C++ (any version) it can create a sample project for you. For that click "New Project" button on the Start Page. Then you'll have to select the type of project. I don't know, if you have no idea what MFC is, then select Win32 Project, otherwise MFC Application. (You'll have to select more stuff before the sample is created).

When done Visual C++ will create a sample program which after compiling will create a window for you. Then you can modify it according to your needs. I'm not sure though, that Visual C++ 2005 Express Edition Beta 2 that you can download for free has a complete support for C++/MFC and samples?

As for capturing arrow keys, simply process WM_KEYDOWN message. You will need to trap it when wParam is VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT. More info here:
It's hard to explain how to create windows outside of MFC. In Win32 you have to call RegisterClassEx first ( and then CreateWindowEx for each window ( You'll have to create WndProc routine to handle all messages as well.
 
You rock like KISS in the 70's. Unfortunately, I won't be able to try it out for a couple of days. Do you happen know where I can download windows.h and winuser.h? I haven't been able to find them anywhere. Thanks and I'll let you know how it goes.
 
Those files as well as other important libraries will be included in Platform SDK folder when you install Visual C++. Otherwise Google for it if you just need those files.
 
I am finally up and running on MS Visual C++ 6. I have already noticed several differences. Do you know what the difference is between Visual C++ and the .NET Framework? Anyway, I have tried to handle the WM_KEYDOWN message. When I was using Visual C++ 2005 Beta, there was an OnKeyPress function that handled ANY key that was pressed. I couldn't find anything like that in C++ 6. MSVC 6 sends me to the OnKeyDown function. That function passes nChar. I have tried to use a switch statement to process the arrow keys, but the VK_XXXX arrow keys aren't recognized. Are the arrow keys system keys? That function only handles non-system keys. The documentation at MSDN talks about the arrow keys being part of the extended keys on enhanced keyboards. Does this make a difference? I saw some other info at
about keyboards. Any ideas what I am doing wrong?

Thanks.
 
Thanks for the info. I've seen some examples like this before, but I'm not real sure exactly how to implement it. Most of the examples are not windows dialog based. I am still trying to learn how to use MS Visual C++ 6. Where exactly do I need to place my code to get it to work. Each MSVC 6 project I start automatically creates several files:

Example.cpp
ExampleDlg.cpp
Example.h
ExampleDlg.h
Example.rc
StdAfx.ccp
StdAfx.h

Does anyone have any easy ways to remember what each of these do or in which file I need to place my new code? As a test, I tried to place the code in the dialog based application (in Example.cpp and ExampleDlg.ccp). When a key down event was captured it was supposed to change the window title. Nothing was captured. Can anyone help a struggling amateur??

Thanks.
 
I am finally up and running on MS Visual C++ 6. I have already noticed several differences
Well, definitely MS Visual C++ 6 is different than Visual C++ 2005 -- it's three versions down :) Version 6 is the older one, but nontheless you can create your apps there too. My preferred one is MS Visual C++ 2002. In 2003 they took away some nice features and added lots of useless cr*p. But, it's my own opinion...

Does anyone have any easy ways to remember what each of these do or in which file I need to place my new code?
Don't concentrate on the meaning of files, all you need there is Class Wizard (Ctrl+W) -- you have to read documentation on that. Also there is a sidebar on the left side of the Visual C++ 6 window called Workspace. (It looks like a tree control.) That's where you navigate thru your project.

Now going back to handling arrow keys, WM_KEYDOWN can handle it too. To add this handler right click on C*****View class in the Workspace window (where **** is the name of your program). Then select Add Windows Message Handler..., then doubleclick on WM_KEYDOWN and click OK. The new function OnKeyDown will appear in that class. Click it's name in the Workspace window to open it. Then edit it to look like this:
Code:
void CTestView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	if(nChar == VK_UP)
	{
		MessageBeep(MB_ICONERROR);
	}

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

Now when you click Up button when your program is running you should hear a sound, that will mean that this method works.
 
I actually tried that before. I could get it to work with the function keys and the SPACE bar, but not with the arrows or with keys like the TAB key. I ended up getting the arrow keys working with this:
Code:
LRESULT CAlphaDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	switch(wParam){

		case 37:{
			SetWindowText("Left Arrow Pressed");
			break;
				}
		case 38:{
			SetWindowText("Up Arrow Pressed");
			break;
				}
		case 39:{
			SetWindowText("Right Arrow Pressed");
			break;
				}
		case 40:{
			SetWindowText("Down Arrow Pressed");
			break;
				}
	}	
	
	return CDialog::DefWindowProc(message, wParam, lParam);
}
Alpha is the name of the project. It seems to work OK. Now I have to figure out how to pass this arrow information to another application via the serial port. I think I'll tackle that problem tomorrow. I'll also need to eventually implement some way to have buttons representing the arrows showing when the key has been depressed. Then I'll need to do a vertical slider control to represent a "throttle/power" control and then pass that info through the serial port. I have my work cut out for me. Thanks for your help on the arrow thing. Please let me know if you have any ideas on this other junk I need to do.

Thanks again.
 
No, this is absolutely wrong! You cannot trap message by its wParam value. OK, you use DefWindowProc to trap your dialog messages but you must limit them to a certain message. In your case any of the hundreds of messages going through this default handler may have wParam value equal to yours - what would happen then?

It must be WM_KEYDOWN, or any other message you want to use. If you don't know what message you trapped with your code above, use debugger to check 'message' value while inside this function.

Also, what is wrong with using VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT instead of integers? This will make your code both readable and portable.

Here's an example of how your code should look like:
Code:
LRESULT CAlphaDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    if(message == WM_KEYDOWN)
    {
        switch(wParam){

            case VK_LEFT:{
                SetWindowText("Left Arrow Pressed");
                break;
                }
            case VK_UP:{
                SetWindowText("Up Arrow Pressed");
                break;
                }
            case VK_RIGHT:{
                SetWindowText("Right Arrow Pressed");
                break;
                }
            case VK_DOWN:{
            SetWindowText("Down Arrow Pressed");
            break;
                }
        }
    }
    
    return CDialog::DefWindowProc(message, wParam, lParam);
}
 
One more thing...
Now I have to figure out how to pass this arrow information to another application via the serial port

Serial port -- Why do you need this?
 
Hey,

Sorry it took me so long to reply...

I tried out that code and couldn't get it to work. I was able to substitute VK_LEFT for 37, etc., and it seems to work fine. And to answer your last question...the overall project is to create a windows based command console for a remote control airplane. The inputs to the window will be sent to an RF transmitter via serial port and then on to the airplane. Eventually I would like to develop the window to have the same instruments that are in the cockpit of an airplane. For now I'll settle for just words and numbers. I am also going to try to incorporate GPS inputs from the airplane as well as a video feed. Seems kind of daunting for a beginner. Please let me know if you have any ideas for me.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top