Generic Keyboard/Remote Input Plugin (1 Viewer)

1stdead

Portal Pro
February 1, 2008
1,089
139
Copenhagen
Home Country
Denmark Denmark
rsenden: I read the link provided above and someone said this:
I ran into the same timeout issue you describe when I was writing my own keyboard hook the other day. To get around the issue, I wrote my hookcallback proc such that it calls a key press event asynchronously and immediately returns.


Code how it was done:
[collapse]
void SomeMethod()
{
KeyboardHook hook = new KeyboardHook("Override minimize all");

string errorMessage;
if (!hook.TrySetKeys(EnhancedKeys.WinLogo | EnhancedKeys.M, out errorMessage))
{
// If another hook already owns these keys, you will get an error here.
MessageBox.Show(errorMessage);
return;
}

// Instead of minimizing all windows, WinLogo + M will now display
// this messagebox.
hook.Pressed += (s, e) =>
{
MessageBox.Show("Pressed!");
};

// Activate the hook.
hook.Engage();

// Sometime later...

// By default, the hook will only allow its Pressed event(s) to execute. To
// allow additional processing, set this property to true. In this example, setting
// AllowPassThrough to true will (1) display a message box and (2) minimize all windows.
hook.AllowPassThrough = true;

// By default, the hook's Pressed event(s) will only execute once per key press. To
// continuously execute these events while the key is held down, set this to true.
hook.AutoRepeat = true;

// Disengage to temporarily disconnect the hook from the system. To re-activate,
// call Engage() again.
hook.Disengage();

// Kills off the hook. Don't forget to call Dispose or the hook's keys will remain
// unavailable until after its finalizer has executed. Dispose() calls Disengage() so
// it is not necessary to call both.
hook.Dispose();

// Most of the rest is just informational
Console.WriteLine("Is alt key used: " + hook.Alt);
Console.WriteLine("Is shift key used: " + hook.Shift);
Console.WriteLine("Is win logo key used: " + hook.Windows);
Console.WriteLine("Is control key used: " + hook.Control);
Console.WriteLine("Current keys: " + hook.Keys);
Console.WriteLine("Current unmodified key: " + hook.UnmodifiedKey);
Console.WriteLine("Is hook active: " + hook.IsEngaged);
Console.WriteLine("Hook has no keys assigned: " + hook.IsEmpty);
}
[/collapse]


Would that be possible?
 

rsenden

Portal Pro
August 22, 2006
88
108
Rotterdam, NL
Home Country
Netherlands Netherlands
rsenden: I read the link provided above and someone said this:
I ran into the same timeout issue you describe when I was writing my own keyboard hook the other day. To get around the issue, I wrote my hookcallback proc such that it calls a key press event asynchronously and immediately returns.

Would that be possible?

Hi, thanks for the tip, I overlooked that post. The KeyboardHook implementation that is used in this example isn't directly usable for our purpose. It does however indeed show an example of how to invoke events asynchronously. If time permits I'll try to implement this in the plug-in.

Unfortunately, testing has shown that even if I remove all processing code from the hook implementation, it is still being disabled by Windows. I've even tried to reduce the hook code to a single timestamp assignment (which is used outside of the hook to check whether the hook is still active). I even removed all logging from the hook (just in case; maybe disk writes take too much time). So, invoking the events asynchronously will not completely solve the problem.
 

1stdead

Portal Pro
February 1, 2008
1,089
139
Copenhagen
Home Country
Denmark Denmark
Thanks for trying that at least. So you did write the code and already did do the testing? I think i missed the connection between the two sections.

I did some more googling :)

Found this:
On Windows 7 we have to make sure that the callback function of the hook can return in less than LowLevelHooksTimeout, which is 300 ms. And we allow for the application to be timed out 10 times when processing the hook callback message. If it times out an 11th time, Windows will unhook the application from the hook chain. This is a by design feature and it was added in Win7 RTM.



My recommendation is that low level hooks should be avoided whenever possible. If you are monitoring keystrokes (and not trying to block them), you can get the keyboard input via Raw Input. This is lighter weight than hooks, won’t affect other apps’ responsiveness, and won’t be turned off if the app isn’t responsive.

Global hooks getting lost on Windows 7 - Decrypt my World - Site Home - MSDN Blogs


More people reported separate thread should fix this:
I have the following workaround:
Create and start during initialization a separate thread which sets the mouse_ll hook and proceeds win message queue,
like this:

Hooking problem in Windows 7


But as it seems it is not a permanent solution....

I did also try and change the regkey as I proposed earlier, and wasn't that much help. Sometimes backkey (as I use for ESC) switces between fullscreen mode. And actually my wireless keyboard/mouse has the same problem. Only restart will help fix it.
 

rsenden

Portal Pro
August 22, 2006
88
108
Rotterdam, NL
Home Country
Netherlands Netherlands
What you explain would be a good solution.

Well, here you are ;). Can you please try the attached DLL (overwrite the one in Program Files\Team MediaPortal\MediaPortal\plugins\process after making a backup copy; make sure that the backup copy doesn't have a .dll extension and/or is moved to a different directory) and see if this one works better? I have implemented faster lookup of mapped keycodes, and asynchronous call to MapAction (which executes the MediaPortal action).

Please note that I only did a couple of quick tests; this DLL shouldn't be considered production-ready. Also note that it will not completely solve the problem; keys that are pressed while MediaPortal is busy doing something will probably still not be captured by the plug-in.

Regarding your statement that everything worked correctly with an older version of MP; are there any other changes in your configuration? For example, some people have reported that the issue occurs more frequent when using specific codecs: https://forum.team-mediaportal.com/...boardhandler-seems-inactive-restarting-89901/
 

rsenden

Portal Pro
August 22, 2006
88
108
Rotterdam, NL
Home Country
Netherlands Netherlands
I did some more googling :)

Found this:
My recommendation is that low level hooks should be avoided whenever possible. If you are monitoring keystrokes (and not trying to block them), you can get the keyboard input via Raw Input. This is lighter weight than hooks, won’t affect other apps’ responsiveness, and won’t be turned off if the app isn’t responsive.

The problem is that we are trying to block keystrokes, so I think this recommendation isn't applicable in our case.

More people reported separate thread should fix this:
I have the following workaround:
Create and start during initialization a separate thread which sets the mouse_ll hook and proceeds win message queue,
like this:

Hooking problem in Windows 7

Together with the faster mapping lookup and asynchronous execution of mapped actions that I already implemented (see my previous post), using a separate thread could indeed solve the problem. However, I'm not completely sure that it will work in our situation, and I'm not sure yet what is the best way to implement this.
 

enanno

Portal Member
April 6, 2011
29
5
Home Country
Spain Spain
What you explain would be a good solution.

Well, here you are ;). Can you please try the attached DLL (overwrite the one in Program Files\Team MediaPortal\MediaPortal\plugins\process after making a backup copy; make sure that the backup copy doesn't have a .dll extension and/or is moved to a different directory) and see if this one works better? I have implemented faster lookup of mapped keycodes, and asynchronous call to MapAction (which executes the MediaPortal action).

Please note that I only did a couple of quick tests; this DLL shouldn't be considered production-ready. Also note that it will not completely solve the problem; keys that are pressed while MediaPortal is busy doing something will probably still not be captured by the plug-in.

Regarding your statement that everything worked correctly with an older version of MP; are there any other changes in your configuration? For example, some people have reported that the issue occurs more frequent when using specific codecs: https://forum.team-mediaportal.com/...boardhandler-seems-inactive-restarting-89901/

Thanks, just tried, man, but I got very weird results.
I think the Left, Right, Up, Down buttons are the only ones working now.
However, if I push the Enter button, for example, (in my remote or my wireless keyboard, it's the same), it happens nothing. I push a lot of times until the 20th or 30th time it works. And the Fullscreen button (Alt+Enter) is always windowing MP now.


Edit:

Ok, I've just made some test with another computer.
I've installed MP and copy my backup (via BackupSettings plugin) and made some tests.

The thing is that from 30 times I push Fullscreen button, it fails only one (in my main computer fails 1 of 3).
So, as somebody said above, my main computer must be with low resources, I think.

I now understand that the problem has always been there, but I couldn't see it.
Now I have to discover what's wrong with my main computer hahahah

I still hope a solution for this wonderful plugin, tho :)
Thanks!
 

Lyfesaver74

Public Relations
  • Premium Supporter
  • September 25, 2010
    1,544
    1,122
    Texas
    Home Country
    United States of America United States of America
    Just wanna say thanks to rsenden and enanno for tinkering and trying to make this great plugin better =) many thanx
     

    rsenden

    Portal Pro
    August 22, 2006
    88
    108
    Rotterdam, NL
    Home Country
    Netherlands Netherlands
    Well, here you are ;). Can you please try the attached DLL and see if this one works better? I have implemented faster lookup of mapped keycodes, and asynchronous call to MapAction (which executes the MediaPortal action).

    Thanks, just tried, man, but I got very weird results.

    Edit:

    Ok, I've just made some test with another computer.

    The thing is that from 30 times I push Fullscreen button, it fails only one (in my main computer fails 1 of 3).

    Regarding the weird results on your main MP installation, do you have a log file for that? On the computer that fails only 1 of 30, did you try both the regular version of the plug-in and the DLL attached in my previous post? Does the test-DLL give the same weird results on that PC, or does it work the same or maybe even better than the regular version?

    I will do some more testing myself, unfortunately I can only test on my development laptop with a regular keyboard (even though that shouldn't make a big difference, it isn't really production-like). My HTPC is still running WinXP so the problem doesn't occur there. If it were running Win7, this plug-in would probably be useless as my HTPC is low-end (Celeron 2.66, 512MB memory) :D.

    If other people are willing to test (or help look into this problem), that would be appreciated.

    Edit:
    I now also encountered the weird behavior. Attached to this post is a zipped DLL that seems to work better. Again, in order to use this DLL, make a backup copy of the original KeyboardInputPlugin.dll in \Program Files\Team MediaPortal\MediaPortal\plugins\process (make sure to give it a non-DLL extension and/or move the backup copy to another directory) and then replace it with the attached DLL. Can you please try it and let me know the result?


    \Edit: Removed DLL; a new version of the plug-in has been released which incorporates these and other changes.
     

    1stdead

    Portal Pro
    February 1, 2008
    1,089
    139
    Copenhagen
    Home Country
    Denmark Denmark
    Thanks. Started testing... Depending on how it goes i'll post my results in this post. Sometimes it takes ages to tricker the "bug".
     

    enanno

    Portal Member
    April 6, 2011
    29
    5
    Home Country
    Spain Spain
    Well, here you are ;). Can you please try the attached DLL and see if this one works better? I have implemented faster lookup of mapped keycodes, and asynchronous call to MapAction (which executes the MediaPortal action).

    Thanks, just tried, man, but I got very weird results.

    Edit:

    Ok, I've just made some test with another computer.

    The thing is that from 30 times I push Fullscreen button, it fails only one (in my main computer fails 1 of 3).

    Regarding the weird results on your main MP installation, do you have a log file for that? On the computer that fails only 1 of 30, did you try both the regular version of the plug-in and the DLL attached in my previous post? Does the test-DLL give the same weird results on that PC, or does it work the same or maybe even better than the regular version?

    I will do some more testing myself, unfortunately I can only test on my development laptop with a regular keyboard (even though that shouldn't make a big difference, it isn't really production-like). My HTPC is still running WinXP so the problem doesn't occur there. If it were running Win7, this plug-in would probably be useless as my HTPC is low-end (Celeron 2.66, 512MB memory) :D.

    If other people are willing to test (or help look into this problem), that would be appreciated.

    Edit:
    I now also encountered the weird behavior. Attached to this post is a zipped DLL that seems to work better. Again, in order to use this DLL, make a backup copy of the original KeyboardInputPlugin.dll in \Program Files\Team MediaPortal\MediaPortal\plugins\process (make sure to give it a non-DLL extension and/or move the backup copy to another directory) and then replace it with the attached DLL. Can you please try it and let me know the result?

    I'm sorry, I uninstalled MediaPortal from "the other" PC. I'll give it a try later.

    Now, here we go with my main PC.

    I've just tried your the second dll you've attached, and put MediaPortal y Debug mode. Here you have my View attachment 92740 file, but I have to tell you that even if it's less agressive than first dll, the second one still does "nothing" when I push Enter. The actions I do are similar than before, I mean, I go to Videos, Películas, choose one movie, and push enter button until at 5th or 7th time it runs. Then I push Fullscreen button and MediaPortal gets windowed again.

    I miss Windows XP hahahah
     

    Users who are viewing this thread

    Top Bottom