Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Keyboard Accelerators

Keyboard Accelerators

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    louis 0
    wrote on last edited by
    #1

    I have a MDI application of CListView. One of the listviews has a maximum of 20 entries(rows) with various column data and settings. In one of the settings I would like the user to configure a keyboard accelerator key or hotkey for each row. When the user double clicks on a row a dialog box opens which allows the user to enter the hotkey in an EditBox. I have read about ACCEL table, CreateAcceleratorTable, TranslateAccelerator and PreTranslateMessage, but is does not make sense to me at this time. Can someone please show what the code for this look like. Thanks :confused:

    M 1 Reply Last reply
    0
    • L louis 0

      I have a MDI application of CListView. One of the listviews has a maximum of 20 entries(rows) with various column data and settings. In one of the settings I would like the user to configure a keyboard accelerator key or hotkey for each row. When the user double clicks on a row a dialog box opens which allows the user to enter the hotkey in an EditBox. I have read about ACCEL table, CreateAcceleratorTable, TranslateAccelerator and PreTranslateMessage, but is does not make sense to me at this time. Can someone please show what the code for this look like. Thanks :confused:

      M Offline
      M Offline
      Mazdak
      wrote on last edited by
      #2

      I don't know if got your problem well,but first you can ovverriden NM_DBCLK of your view,and inside it create your dialog class,and in your dialog class use WM_SETHOTKEY or WM_SYSCOMMAND Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

      L 1 Reply Last reply
      0
      • M Mazdak

        I don't know if got your problem well,but first you can ovverriden NM_DBCLK of your view,and inside it create your dialog class,and in your dialog class use WM_SETHOTKEY or WM_SYSCOMMAND Mazy Don't Marry a Person You Can Live With... Marry Someone You Can Not Live Without

        L Offline
        L Offline
        louis 0
        wrote on last edited by
        #3

        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 Louis :confused:

        S 1 Reply Last reply
        0
        • L louis 0

          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 Louis :confused:

          S Offline
          S Offline
          Sef Tarbell
          wrote on last edited by
          #4

          As far as I can tell from my experimentation with the accelerators functions, you need to do something like the following. In my example, the user would hold down Ctrl and a key they have assigned to the item. you have the following member variables in your header file: ACCEL* m_pAccel; //pointer to an array of ACCEL structs HACCEL m_hAccel; //the actual accel table int m_count; //number of accelerators in your implementation file: //your constructor void MyListView :: MyListView () { m_count = 20; m_pAccel = new ACCEL[count]; } //your handler for double click void MyListView :: OnDblClk(....,....) { int index = HitTest(....,....); ... // store hotkey information pAccel[index].fVirt = FCONTROL|FVIRTKEY; //virtual keys work best imho pAccel[index].key = VK_1; //the key pAccel[index].cmd = IDM_FIRST_ROW; //a menu id ... //unregister the hotkeys ::DestroyAcceleratorTable( hAccel ); //register the hotkeys hAccel = ::CreateAcceleratorTable( pAccel, count ); } //your handler for pre-translate message BOOL MyListView :: PreTranslateMessage(MSG* pMsg) { ::TranslateAccelerator( this->GetSafeHwnd(), hAccel, pMsg ); return CListView::PreTranslateMessage(pMsg); } Good luck. ;) Sef Tarbell "A mind all logic is like a knife all blade, it makes the hand bleed that wields it." --Rabindranath Tagore

          L 1 Reply Last reply
          0
          • S Sef Tarbell

            As far as I can tell from my experimentation with the accelerators functions, you need to do something like the following. In my example, the user would hold down Ctrl and a key they have assigned to the item. you have the following member variables in your header file: ACCEL* m_pAccel; //pointer to an array of ACCEL structs HACCEL m_hAccel; //the actual accel table int m_count; //number of accelerators in your implementation file: //your constructor void MyListView :: MyListView () { m_count = 20; m_pAccel = new ACCEL[count]; } //your handler for double click void MyListView :: OnDblClk(....,....) { int index = HitTest(....,....); ... // store hotkey information pAccel[index].fVirt = FCONTROL|FVIRTKEY; //virtual keys work best imho pAccel[index].key = VK_1; //the key pAccel[index].cmd = IDM_FIRST_ROW; //a menu id ... //unregister the hotkeys ::DestroyAcceleratorTable( hAccel ); //register the hotkeys hAccel = ::CreateAcceleratorTable( pAccel, count ); } //your handler for pre-translate message BOOL MyListView :: PreTranslateMessage(MSG* pMsg) { ::TranslateAccelerator( this->GetSafeHwnd(), hAccel, pMsg ); return CListView::PreTranslateMessage(pMsg); } Good luck. ;) Sef Tarbell "A mind all logic is like a knife all blade, it makes the hand bleed that wields it." --Rabindranath Tagore

            L Offline
            L Offline
            louis 0
            wrote on last edited by
            #5

            Thanks. It all makes sense now. Cheers. Louis :laugh:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups