Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable accelerator table items in MFC

Tags:

c++

mfc

I need to temporarily disable a few items from an accelerator table when the input focus is on a CEdit field.

My application has some commands associated with keyboard keys (A, S, D, etc.) and I need to disable those while the user is entering text in the field.

like image 541
Fábio Avatar asked Sep 22 '10 19:09

Fábio


2 Answers

You could try CopyAcceleratorTable to get the ARRAY of ACCEL structures then edit out the ones you don't want, Call DEstroyAcceleratorTable on the current table. Then use CreateAcceleratorTable to create the new table with the edited accelerator table.

Edit: This link may be useful.

like image 176
Goz Avatar answered Oct 19 '22 17:10

Goz


The answer from Goz works very fine. To save all other people some time, here is sample code that follows his suggestion:

// Allocate the accelerator buffer
HACCEL hAccelOld = LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACC_TECONTROL));
int iNumAccelerators = CopyAcceleratorTable(hAccelOld, NULL, 0);   
ACCEL *pAccels = new ACCEL[iNumAccelerators];

// Copy the current table to the buffer
VERIFY(CopyAcceleratorTable(hAccelOld, pAccels, iNumAccelerators) == iNumAccelerators);

// Modify the pAccels array as required
...

// Destroy the current table resource...
VERIFY(DestroyAcceleratorTable(hAccelOld) == TRUE);

// ... create a new one, based on our modified table
m_hTerAcceleratorTable = CreateAcceleratorTable(pAccels, iNumAccelerators); 
ASSERT(m_hTerAcceleratorTable != NULL);

// Cleanup
delete[] pAccels;
like image 35
Fabian Avatar answered Oct 19 '22 17:10

Fabian