• Advertisement
Sign in to follow this  

Unity Windows hooks (e.g. WH_KEYBOARD_LL) must be in DLL?

This topic is 4945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is in reference to "Quick question about VK_SNAPSHOT" - the board doesn't seem to allow replies to old threads. OP probably doesn't care anymore, but I had the same problem and found this thread while searching. VK_SNAPSHOT apparently isn't delivered by WM_KEYDOWN or even WM_SYSKEYDOWN messages. What you need to do is register a WH_KEYBOARD_LL hook. If VK_SNAPSHOT is received there, post a corresponding WM_KEYDOWN message to your wndproc, and don't chain to the next hook; return > 0. This prevents the normal Windows copy-to-clipboard behavior. One open question is whether the hook proc really needs to be in a DLL, as documented - it would be nice to avoid, if possible. I found an article claiming it's a non-issue, but I'm not sure the author fully understood the problem. First, I RTFM:
The LowLevelKeyboardProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system calls this function every time a new keyboard input event is about to be posted into a thread input queue. The keyboard input can come from the local keyboard driver or from calls to the keybd_event function. If the input comes from a call to keybd_event, the input was "injected". However, the WH_KEYBOARD_LL hook is not injected into another process. Instead, the context switches back to the process that installed the hook and it is called in its original context. Then the context switches back to the application that generated the event.
OK. Does this mean the hook is always called in the address space of the application that issued the hook, or is this only special-cased when someone else calls keybd_event? Comment from the above article:
this approach can ONLY be used with WH_MOUSE_LL and WH_KEYBOARD_LL [referring to dox above] That why this works so good. Try to do this with another hooks and you'll see the back of the coin.
The author replies:
lol, that refers to something completely different, it has to do with injected events generated by API calls like keybd_event(). You seriously got it wrong, and I am so very certain about this because I already used the same code with all kinds of hooks already, and it always worked.
So, if there is no special context-switch handling, I'm not sure how global hooks are to work with this method (adding the hook proc to the export table and passing the application's module handle). Obviously the hook proc will have to be mapped into each process in whose context a hook event may be generated. The EXE will have to be loaded and the hook proc imported. Now a big problem: I don't think EXEs get a separate IAT, as would a DLL. That means one of the 2 instances has an invalid IAT => catastrophe, since they won't be mapped at the same address. Another twist: I tried it out and the keyboard hook just works, even if the proc is in my app and it's not exported. A possible explanation would be that SetWindowsHookEx calculates the RVA (proc - module_inst) and uses that instead of PE exports. Or does the fact that it's exported cause Windows to map the proc in some shared memory? It is not the case that module_inst == 0x400000 (i.e. normal executable instead of DLL) => the hook is treated as local, since the hook still fires even if other apps have the focus. To summarize, both explanations are plausible: special handling to call the hook in its app's address space, or mapping the hook proc into each app ("importing" via stored RVA). Is it therefore safe to leave out the DLL, in contradiction to the dox? I see that glfw does so without any comment. Anyone know more, or been bitten by this?

Share this post

Link to post
Share on other sites
No one here using WH_KEYBOARD_LL?

Looks like I'll stick with the interpretation that Windows calls the LL hooks in their original context - it does seem to work.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By RoKabium Games
      Another one of our new UI for #screenshotsaturday. This is the inventory screen for showing what animal fossils you have collected so far. #gamedev #indiedev #sama
    • By eldwin11929
      We're looking for programmers for our project.
      Our project is being made in Unity
      -Skills in Unity
      We're looking for programmers who can perform a variety of functions on our project.
      Project is a top-down hack-and-slash pvp dungeon-crawler like game. Game is entirely multiplayer based, using randomized dungeons, and a unique combat system with emphasis on gameplay.
      We have a GDD to work off of, and a Lead Programmer you would work under.
      Assignments may include:
      -Creating new scripts of varying degrees specific to the project (mostly server-side, but sometimes client-side)
      -Assembling already created monsters/characters with existing or non-existing code.
      -Creating VFX
      -Assembling already created environment models
      If interested, please contact: eldwin11929@yahoo.com
      This project is unpaid, but with royalties.
      Additional Project Info:
      Bassetune Reapers is a Player-verus-Player, competitive dungeon crawler. This basically takes on aspects of dungeon crawling, but with a more aggressive setting. Players will have the option to play as the "dungeon-crawlers" (called the 'Knights', or "Knight Class", in-game) or as the "dungeon" itself (literally called the 'Bosses', or "Boss Class", in-game). What this means is that players can choose to play as the people invading the dungeon, or as the dungeon-holders themselves.
      Key Features:
      -Intense, fast-paced combat
      -Multiple skills, weapons, and ways to play the game
      -Tons of different Bosses, Minibosses, creatures and traps to utilize throughout the dungeon
      -Multiple unique environments
      -Interesting, detailed lore behind both the game and world
      -Intricate RPG system
      -Ladder and ranking system
      -Lots of customization for both classes s of customization for both classes
    • By RoKabium Games
      Custom coffee mugs have arrived... More caffeine!
      Have a great weekend everyone! 
      #gamedev #indiedev #sama #caffeine
    • By Atwo Studios
      Hey guys,

      Anthony here from Atwo Studios bringing you some new updates for the new year!
      In this video I go over our game ROY, the new games and some general updates to the company!

      If you have not checked out ROY feel free to give it a try! Many people have said they enjoyed the game thus far!
      ROY: https://goo.gl/o6JJ5P
    • By Affgoo
      still a lot of work to do, but its pretty stable  please let me know what you think <3
      Atlas Sentry is a game of destroy everything. Using your turret, simply swivel and shoot your way to victory, upgrading your weapons to unleash destruction on the variety of spaceships. The bigger your combo’s the more score you get! Earn silver as you play and then purchase new weapons and abilities to better deal with your enemy. Different enemies use different tactics and weapons, work out your own priorities in their destruction order. 

      **2 different game modes 
      **A level select mode with 20 difficult levels including a final boss, can you defeat it? **Arcade mode of endless destruction, how long will you last? 
      **High scores to compete against others, see who can take the top spot. 
  • Advertisement