Home » Category » Microsoft Visual C & C++

Microsoft Visual C & C++: How do I configure accelerator keys during run-time

202| Thu, 07 Feb 2008 09:06:00 GMT| bamboo| Comments (5)
I have a MDI application of CListViews.
In one of the ListViews I would like the user to configure he's/her own hotkeys which will activate a certain toolbar button.

The hotkey is presently captured in an editbox.

Can someone please show me how would I achieve this?

Thanks.

Cheers
Louis

-- i always rate --

Keywords & Tags: configure, accelerator, keys, during, run-time, microsoft, visual c++, vc

URL: http://www.7prog.com/visual-c-c++/139540/
 
«« Prev - Next »» 5 helpful answers below.
Hi,
If you are looking for how to dynamically create Accelerator Table then you have to use CreateAcceleratorTable / LoadAccelerators & DestroyAcceleratorTable

and in MSDN you have code samples for all these

Hope this helps,

Kind Regards,
Natarajan.

http://Win32programming.tripod.com

t_natarajan | Sat, 10 Nov 2007 07:47:00 GMT |

Thanks. I do appreciate you input.

I still have no joy though. I read the info on the MSDN but it makes little sense to me. The sample code are not relevant to MFC. It could be that I do not have the latest MSDN Cds.

I am not sure whether I am way off the tracks,
but would appreciate any help on this.

This is what I have in mind at the moment :

typedef struct _Accel {
BYTE fVirt;
WORD Key;
WORD cmd;
}Accel;

class MyListView : public CListView {
public :
Accel *pAccel;
HACCEL haccel;
int count; // number of hotkeys can
// be defined
....
};

// in the CPP file

// in class constructor
void MyListView :: MyListView () {
count = 20;
haccel = CreateAcceleratorTable(pAccel, count);
}

void MyListView :: OnDblclk (..., ...) {
// assign hotkey string to structure
...
// I DO NOT KNOW HOW TO ASSIGN THE HOTKEY TO THE
// PARTICULAR ID RESOURCE NAME
...
}

void MyListView :: OnKeyDown (...)
{
// read the key being pressed and activate the neccessary toolbar button

HOW IS THIS DONE

}

// DestroyAcceleratorTable when application closes
...
...



Thanks

Bamboo

-- i always rate --

bamboo | Sat, 10 Nov 2007 07:48:00 GMT |

Thanks. I do appreciate you input.

I still have no joy though. I read the info on the MSDN but it makes little sense to me. The sample code are not relevant to MFC. It could be that I do not have the latest MSDN Cds.

I am not sure whether I am way off the tracks,
but would appreciate any help on this.

This is what I have in mind at the moment :

typedef struct _Accel {
BYTE fVirt;
WORD Key;
WORD cmd;
}Accel;

class MyListView : public CListView {
public :
Accel *pAccel;
HACCEL haccel;
int count; // number of hotkeys can
// be defined
....
};

// in the CPP file

// in class constructor
void MyListView :: MyListView () {
count = 20;
haccel = CreateAcceleratorTable(pAccel, count);
}

void MyListView :: OnDblclk (..., ...) {
// assign hotkey string to structure
...
// I DO NOT KNOW HOW TO ASSIGN THE HOTKEY TO THE
// PARTICULAR ID RESOURCE NAME
...
}

void MyListView :: OnKeyDown (...)
{
// read the key being pressed and activate the neccessary toolbar button
....
....
HOW IS THIS DONE
....
....

}

// DestroyAcceleratorTable when application closes
...
...
...



Thanks

Bamboo

-- i always rate --

bamboo | Sat, 10 Nov 2007 07:49:00 GMT |

Hi,

Well, I am not too familiar with the MFC world but I can tell you how in plain Win32 in the main Message loop you have to use the TranslateAccelator something like this before

while (GetMessage(&msg, (HWND) NULL, 0, 0))
{
// Check for accelerator keystrokes.

if (!TranslateAccelerator(
hwndMain, // handle to receiving window
haccel, // handle to active accelerator table
&msg)) // address of message data
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

now if haccel points to your Accelerator table then all the magic is done !

Sorry I am do not know MFC and how to use this in MFC.

Kind Regards,
Natarajan.

http://Win32programming.tripod.com

t_natarajan | Sat, 10 Nov 2007 07:50:00 GMT |

Hi there,
Thanks anyway.
I have gone through my code again and saw that I have made some basic mistakes and have learned some new moves - if one can called it that.

the new code is as follows :



class MyListView : public CListView
{
public :
Accel m_Accel[20]; // note that Accel is
// already defined - no need to define the
// structure again
HACCEL haccel;
int count; // number of hotkeys that can
// be defined....
};

// in the CPP file
// in class constructor
void MyListView :: MyListView ()
{
count = 20;
haccel = CreateAcceleratorTable(m_Accel, count);
}

void MyListView :: ~MyListView ()
{
DestroyAcceleratorTable(m_hAccel);
}

void MyListView :: OnDblclk (..., ...)
{
// assign hotkey string to structure...
// The relevant row data is stored in array
// it seems that no ID resource name would be
// needed
// first read checkbox and combobox for
// hotkey format,
//
.......
//
// now store the hotkey settings
//
m_Accel[RowNumber].fVirt = szfVirtStr;
m_Accel[RowNumber].Key = szKeyStr;
m_Accel[RowNumber].cmd = 1; // will be ignored

...
}

void MyListView :: PreTranslateMessage (MSG *pMsg)
{

for (nIndex=0; nIndex <20; nIndex++)
{
// Read the hotkey press

..... HOW IS THIS DONE........
}

}



Thanks

Bamboo

-- i always rate --

bamboo | Sat, 10 Nov 2007 07:51:00 GMT |

Microsoft Visual C & C++ Hot Answers

Microsoft Visual C & C++ New questions

Microsoft Visual C & C++ Related Categories