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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use F1 to F12 KEYs for shortcut of a Button 1

Status
Not open for further replies.

youssef

Programmer
Mar 13, 2001
96
BE
Hi,
I would like to use the F1 to F12 keys for shortcut of 12 buttons.

How can I do this ?

Must I use the WM_CHAR for it ?

Best Regards
 
I use CreateAcceleratorTable to do this.

// in the initialization part of the program:

#define NumberOfAccelerators 12
#define MessageBase <SOME-NUMBER>

ACCEL Accel[NumberOfAccelerators];
HACCEL hAccel = NULL;

for ( DWORD i1 = 0; i1 < NumberOfAccelerators; i1++ )
{ Accel[i1].fVirt = FNOINVERT | FVIRTKEY;
Accel[i1].cmd = MessageBase + i1;
Accel[i1].key = 112 + i1; }

hAccel = CreateAcceleratorTable
( &Accel, NumberOfAccelerators );


// In the Windows procedure :

case WM_COMMAND :
{ WORD w1 = LOWORD ( wParam );
if ( w1 >= MessageBase &&
w1 < ( MessageBase + NumberOfAccelerators ))
{ // f1 .. f12 pressed
Your code for the function keys here
} else
{ // rest of WM_COMMAND
}
}
break;

// In the ending of the program :

if ( hAccel != NULL )
{ DestroyAcceleratorTable ( hAccel );
hAccel = NULL; }

 
Forgot something.

In the message loop:

while ( GetMessage ( &Msg, ... ))
{ if ( !TranslateAccelerator ( hWnd, hAccel, &Msg ))
{ TranslateMessage ( &Msg );
DispatchMessage ( &Msg ); } }

Marcel
 
Thank for the answer.

But I don't understand this :

1/ // In the Windows procedure :
where I can insert it ? In WM_COMMAND or other ?

2/ //In the message loop:

where I can insert it ? In ONPRETRANSLATEMESSAGE or other ?

Best Regards

 
Youssef,

I understand you use AppWizard or something like that to make an MFC application. (I don't so I do not know anything of ONPRETRANSLATEMESSAGE, which is MFC I suppose). But if you use AppWizard, you can do this using resources. Insert a new resource, type Accelerator. Choose an unique ID, at the Key field, fill in VK_F1 .. VK_F12, uncheck Ctrl, Alt and Shift and choose Type VirtKey. Do this 12 times, one for each function key.

If you press one of the function keys while the program is running, a WM_COMMAND message is generated, where LOWORD(wParam) == the unique ID-value you have chosen. AT that point you must take the action you have in mind for the function key.

Marcel


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top