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. Include issues

Include issues

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
15 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.
  • S Stuart Dootson

    What you probably want is a thread level keyboard hook that looks at incoming keyboard events and blocks/unblocks input as desired (i.e. does what your keyblocker.cpp does). See this introduction to Windows hooks[^].

    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

    G Offline
    G Offline
    gamefreak2291
    wrote on last edited by
    #5

    ive already attempted to write a keyboard hook and a .dll file, i didnt get either to work though. Let the record show that i am not a programming student yet, im simply trying to get very familiar before i start next year. so im going off of the little bit ive read in my books and internet. so im sorry if i seem very incompetent, but im sure ill get it eventually. so anyhow, i think the issue with my system("filepath"): command not working is because i had to switch from my xp machine to my vista machine and some changes between the two may be the reason why its not working so ill test he system part on my other pc tomorrow

    S 1 Reply Last reply
    0
    • G gamefreak2291

      ive already attempted to write a keyboard hook and a .dll file, i didnt get either to work though. Let the record show that i am not a programming student yet, im simply trying to get very familiar before i start next year. so im going off of the little bit ive read in my books and internet. so im sorry if i seem very incompetent, but im sure ill get it eventually. so anyhow, i think the issue with my system("filepath"): command not working is because i had to switch from my xp machine to my vista machine and some changes between the two may be the reason why its not working so ill test he system part on my other pc tomorrow

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #6

      The thing is that BlockInput only blocks the input queue of the thread it's executed in - at least that's what the documentation[^] says, so your separate process implementation can't work, as it can only block its own input. Also - a keyboard hook doesn't need a DLL if it's just for your current thread. The following code adds a hook to the calling thread that blocks input if the 'A' key is pressed. To activate it, you just need to call the StartBlockingFilter function.

      HHOOK myHook = 0;

      LRESULT CALLBACK KeyboardFilter(int code, WPARAM wParam, LPARAM lParam)
      {
      if (wParam == 'a' || wParam == 'A')
      BlockInput(TRUE);
      return CallNextHookEx(myHook, code, wParam, lParam);
      }

      void StartBlockingFilter()
      {
      myHook = ::SetWindowsHookEx(WH_KEYBOARD, &KeyboardFilter, 0, GetCurrentThreadId());
      }

      void FinishBlockingFilter()
      {
      BlockInput(FALSE);
      ::UnhookWindowsHookEx(myHook);
      }

      You could put this code in a .cpp file, #include it and call StartBlockingFilter from where it was included.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      G 1 Reply Last reply
      0
      • S Stuart Dootson

        The thing is that BlockInput only blocks the input queue of the thread it's executed in - at least that's what the documentation[^] says, so your separate process implementation can't work, as it can only block its own input. Also - a keyboard hook doesn't need a DLL if it's just for your current thread. The following code adds a hook to the calling thread that blocks input if the 'A' key is pressed. To activate it, you just need to call the StartBlockingFilter function.

        HHOOK myHook = 0;

        LRESULT CALLBACK KeyboardFilter(int code, WPARAM wParam, LPARAM lParam)
        {
        if (wParam == 'a' || wParam == 'A')
        BlockInput(TRUE);
        return CallNextHookEx(myHook, code, wParam, lParam);
        }

        void StartBlockingFilter()
        {
        myHook = ::SetWindowsHookEx(WH_KEYBOARD, &KeyboardFilter, 0, GetCurrentThreadId());
        }

        void FinishBlockingFilter()
        {
        BlockInput(FALSE);
        ::UnhookWindowsHookEx(myHook);
        }

        You could put this code in a .cpp file, #include it and call StartBlockingFilter from where it was included.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        G Offline
        G Offline
        gamefreak2291
        wrote on last edited by
        #7

        Uh, so I attempted to add that code into a .ccp file, but it failed to compile properly afterwards. I don't know whether there were things I needed to change or add to it because I've never gone over keyboard hooks before. Terribly sorry for all the issues..

        S 1 Reply Last reply
        0
        • G gamefreak2291

          Uh, so I attempted to add that code into a .ccp file, but it failed to compile properly afterwards. I don't know whether there were things I needed to change or add to it because I've never gone over keyboard hooks before. Terribly sorry for all the issues..

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #8

          You'd need to #include <Windows.h> before that code.

          gamefreak2291 wrote:

          Uh, so I attempted to add that code into a .ccp file, but it failed to compile properly afterwards

          What errors did you get?

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          G 1 Reply Last reply
          0
          • S Stuart Dootson

            You'd need to #include <Windows.h> before that code.

            gamefreak2291 wrote:

            Uh, so I attempted to add that code into a .ccp file, but it failed to compile properly afterwards

            What errors did you get?

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            G Offline
            G Offline
            gamefreak2291
            wrote on last edited by
            #9

            #include #include #include using namespace std;

            int main(void)
            {

            HHOOK myHook = 0;

            LRESULT CALLBACK KeyboardFilter(int code, WPARAM wParam, LPARAM lParam)
            {
            if (wParam == 'a' || wParam == 'A')
            BlockInput(TRUE);
            return CallNextHookEx(myHook, code, wParam, lParam);
            }

            void StartBlockingFilter()
            {
            myHook = ::SetWindowsHookEx(WH_KEYBOARD, &KeyboardFilter, 0, GetCurrentThreadId());
            }

            void FinishBlockingFilter()
            {
            BlockInput(FALSE);
            ::UnhookWindowsHookEx(myHook);
            }
            }

            return 0;
            }

            There's the code I used, and the errors I got are as follows:

            H:\Programs\Worthwhile Programs\hidden\hooker.cpp In function `int main()':
            12 H:\Programs\Worthwhile Programs\hidden\hooker.cpp a function-definition is not allowed here before '{' token
            18 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected primary-expression before "void"
            18 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected `;' before "void"
            23 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected primary-expression before "void"
            23 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected `;' before "void"
            23 H:\Programs\Worthwhile Programs\hidden\hooker.cpp At global scope:
            30 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected unqualified-id before "return"
            30 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected `,' or `;' before "return"
            31 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected declaration before '}' token

            S 1 Reply Last reply
            0
            • G gamefreak2291

              #include #include #include using namespace std;

              int main(void)
              {

              HHOOK myHook = 0;

              LRESULT CALLBACK KeyboardFilter(int code, WPARAM wParam, LPARAM lParam)
              {
              if (wParam == 'a' || wParam == 'A')
              BlockInput(TRUE);
              return CallNextHookEx(myHook, code, wParam, lParam);
              }

              void StartBlockingFilter()
              {
              myHook = ::SetWindowsHookEx(WH_KEYBOARD, &KeyboardFilter, 0, GetCurrentThreadId());
              }

              void FinishBlockingFilter()
              {
              BlockInput(FALSE);
              ::UnhookWindowsHookEx(myHook);
              }
              }

              return 0;
              }

              There's the code I used, and the errors I got are as follows:

              H:\Programs\Worthwhile Programs\hidden\hooker.cpp In function `int main()':
              12 H:\Programs\Worthwhile Programs\hidden\hooker.cpp a function-definition is not allowed here before '{' token
              18 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected primary-expression before "void"
              18 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected `;' before "void"
              23 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected primary-expression before "void"
              23 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected `;' before "void"
              23 H:\Programs\Worthwhile Programs\hidden\hooker.cpp At global scope:
              30 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected unqualified-id before "return"
              30 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected `,' or `;' before "return"
              31 H:\Programs\Worthwhile Programs\hidden\hooker.cpp expected declaration before '}' token

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #10

              OK....you needed to put the code in your main.cpp, NOT in the main function - you aren't allowed to define nested functions in C++.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              G 1 Reply Last reply
              0
              • S Stuart Dootson

                OK....you needed to put the code in your main.cpp, NOT in the main function - you aren't allowed to define nested functions in C++.

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                G Offline
                G Offline
                gamefreak2291
                wrote on last edited by
                #11

                Sigh, alright I added the code into my main file and I got it to compile without errors after one simple tweak. I then added in LONG StartBlockingFilter into a spot of my main() function and it is still compiling properly, however its not blocking input when I press a or A. I'm thinking its going to be a major issue.. I'm thinking that the keyboard hook is not going to work with MessageBoxes, which my entire code revolves around. I don't know if the Message Boxes are the issue are not, i hope not.

                S 1 Reply Last reply
                0
                • G gamefreak2291

                  Sigh, alright I added the code into my main file and I got it to compile without errors after one simple tweak. I then added in LONG StartBlockingFilter into a spot of my main() function and it is still compiling properly, however its not blocking input when I press a or A. I'm thinking its going to be a major issue.. I'm thinking that the keyboard hook is not going to work with MessageBoxes, which my entire code revolves around. I don't know if the Message Boxes are the issue are not, i hope not.

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #12

                  gamefreak2291 wrote:

                  I'm thinking that the keyboard hook is not going to work with MessageBoxes

                  The keyboard hook works OK - the BlockInput, however, doesn't. You can, however, simulate BlockInput by using keyboard and mouse hooks to stop messages getting through - this code stops mouse and keyboard messages being sent to the message box:

                  HHOOK myKeyboardHook = 0;
                  HHOOK myMouseHook = 0;
                  bool blocking = false;

                  LRESULT CALLBACK KeyboardFilter(int nCode, WPARAM wParam, LPARAM lParam)
                  {
                  if (nCode < 0) return CallNextHookEx(myKeyboardHook, nCode, wParam, lParam);
                  if (wParam == 'a' || wParam == 'A')
                  blocking = true;
                  else if (wParam == 'b' || wParam == 'B')
                  blocking = false;
                  if (blocking) return 1;
                  return CallNextHookEx(myKeyboardHook, nCode, wParam, lParam);
                  }

                  LRESULT CALLBACK MouseFilter(int nCode, WPARAM wParam, LPARAM lParam)
                  {
                  if (nCode < 0 || !blocking) return CallNextHookEx(myMouseHook, nCode, wParam, lParam);
                  return 1;
                  }
                  void StartBlockingFilter()
                  {
                  myKeyboardHook = ::SetWindowsHookEx(WH_KEYBOARD, &KeyboardFilter, 0, GetCurrentThreadId());
                  myMouseHook = ::SetWindowsHookEx(WH_MOUSE, &MouseFilter, 0, GetCurrentThreadId());
                  }

                  void FinishBlockingFilter()
                  {
                  ::UnhookWindowsHookEx(myKeyboardHook);
                  ::UnhookWindowsHookEx(myMouseHook);
                  }

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  G 1 Reply Last reply
                  0
                  • S Stuart Dootson

                    gamefreak2291 wrote:

                    I'm thinking that the keyboard hook is not going to work with MessageBoxes

                    The keyboard hook works OK - the BlockInput, however, doesn't. You can, however, simulate BlockInput by using keyboard and mouse hooks to stop messages getting through - this code stops mouse and keyboard messages being sent to the message box:

                    HHOOK myKeyboardHook = 0;
                    HHOOK myMouseHook = 0;
                    bool blocking = false;

                    LRESULT CALLBACK KeyboardFilter(int nCode, WPARAM wParam, LPARAM lParam)
                    {
                    if (nCode < 0) return CallNextHookEx(myKeyboardHook, nCode, wParam, lParam);
                    if (wParam == 'a' || wParam == 'A')
                    blocking = true;
                    else if (wParam == 'b' || wParam == 'B')
                    blocking = false;
                    if (blocking) return 1;
                    return CallNextHookEx(myKeyboardHook, nCode, wParam, lParam);
                    }

                    LRESULT CALLBACK MouseFilter(int nCode, WPARAM wParam, LPARAM lParam)
                    {
                    if (nCode < 0 || !blocking) return CallNextHookEx(myMouseHook, nCode, wParam, lParam);
                    return 1;
                    }
                    void StartBlockingFilter()
                    {
                    myKeyboardHook = ::SetWindowsHookEx(WH_KEYBOARD, &KeyboardFilter, 0, GetCurrentThreadId());
                    myMouseHook = ::SetWindowsHookEx(WH_MOUSE, &MouseFilter, 0, GetCurrentThreadId());
                    }

                    void FinishBlockingFilter()
                    {
                    ::UnhookWindowsHookEx(myKeyboardHook);
                    ::UnhookWindowsHookEx(myMouseHook);
                    }

                    Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                    G Offline
                    G Offline
                    gamefreak2291
                    wrote on last edited by
                    #13

                    Alright, I threw in the new bit of code and it also compiled correctly. However I still can't get it to block input when I press a or A. I'm not sure if I'm calling it correctly, I'm trying to call it as follows:

                    { LONG StartBlockingFilter;
                    MessageBox(NULL, "Welcome to my program", "Welcome", MB_OK);
                    MessageBox(NULL, "Just run through all the messages\n" "and you'll be done", "Instructions", MB_OK);
                    MessageBox(NULL, "Oh, and a word of warning.. I wouldn't\n" "suggest using Ctrl+Alt+Del, Alt+Tab\n"
                    "the Windows keys, the Escape keys\n" "or your mouse..", "Warning", MB_OK);}

                    Is that the correct way to call it into use?

                    S 1 Reply Last reply
                    0
                    • G gamefreak2291

                      Alright, I threw in the new bit of code and it also compiled correctly. However I still can't get it to block input when I press a or A. I'm not sure if I'm calling it correctly, I'm trying to call it as follows:

                      { LONG StartBlockingFilter;
                      MessageBox(NULL, "Welcome to my program", "Welcome", MB_OK);
                      MessageBox(NULL, "Just run through all the messages\n" "and you'll be done", "Instructions", MB_OK);
                      MessageBox(NULL, "Oh, and a word of warning.. I wouldn't\n" "suggest using Ctrl+Alt+Del, Alt+Tab\n"
                      "the Windows keys, the Escape keys\n" "or your mouse..", "Warning", MB_OK);}

                      Is that the correct way to call it into use?

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #14

                      Noooo - you need to call StartBlockingFilter, not declare a variable of that name:

                      { StartBlockingFilter();
                      MessageBox(NULL, "Welcome to my program", "Welcome", MB_OK);
                      MessageBox(NULL, "Just run through all the messages\n" "and you'll be done", "Instructions", MB_OK);
                      MessageBox(NULL, "Oh, and a word of warning.. I wouldn't\n" "suggest using Ctrl+Alt+Del, Alt+Tab\n"
                      "the Windows keys, the Escape keys\n" "or your mouse..", "Warning", MB_OK);}

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      G 1 Reply Last reply
                      0
                      • S Stuart Dootson

                        Noooo - you need to call StartBlockingFilter, not declare a variable of that name:

                        { StartBlockingFilter();
                        MessageBox(NULL, "Welcome to my program", "Welcome", MB_OK);
                        MessageBox(NULL, "Just run through all the messages\n" "and you'll be done", "Instructions", MB_OK);
                        MessageBox(NULL, "Oh, and a word of warning.. I wouldn't\n" "suggest using Ctrl+Alt+Del, Alt+Tab\n"
                        "the Windows keys, the Escape keys\n" "or your mouse..", "Warning", MB_OK);}

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        G Offline
                        G Offline
                        gamefreak2291
                        wrote on last edited by
                        #15

                        Alright I switched it to StartBlockingFilter(); but it still did not work but it still is not blocking the input when I press a or A. I also sent you and e-mail explaining more. Respond if you want

                        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