Capure skip button and then custom skip?

A place for App developers to hang out / post
Post Reply
mrsilver

Posts: 63
Joined: Mon Oct 20, 2014 5:01 pm
Location:

HTPC Specs: Show details

Capure skip button and then custom skip?

#1

Post by mrsilver » Sat Oct 25, 2014 6:25 pm

Hi,

Does anyone know if it is possible to capture the skip forward and backwards commands and then do something else with it?

I'd like to make the skip buttons actually jump forward/backwards a custom set of seconds depending on some logic. My original idea was to just catch the button presses in a small external app and modify the skip intervals in the registry, but you need to restart MCE for those to be applied.

My next plan would be to override the default mechanism and then code a replacement. The MediaTransport Class seems to have everything I need to do this, but I cannot work out how I would prevent the original functionality from also occuring. Also the claim that the object model is not accessible from a Windows service or any other process other than those started by Windows Media Center - makes me think my project is somewhat tricky.

Any thoughts?

mdavej

Posts: 1477
Joined: Mon Aug 20, 2012 6:52 pm
Location:

HTPC Specs: Show details

#2

Post by mdavej » Sat Oct 25, 2014 9:05 pm

Could you not simply use a 2 command macro? Set the interval small, then issue a number then skip. For example, set the interval to 10 sec. Then 7 followed by Skip FWD would jump 70 seconds. I have a commercial skip macro which is 6, Skip FWD, giving me a 3 minute skip using the default 30 sec skip.

crawfish

Posts: 465
Joined: Fri Jan 13, 2012 5:16 am
Location:

HTPC Specs: Show details

#3

Post by crawfish » Sat Oct 25, 2014 9:14 pm

It's definitely possible to capture any remote button and send custom commands, including arbitrary skips, with Autohotkey, but what is the logic you have in mind in "depending on some logic?" The latter would be the tricky part.

mrsilver

Posts: 63
Joined: Mon Oct 20, 2014 5:01 pm
Location:

HTPC Specs: Show details

#4

Post by mrsilver » Sun Oct 26, 2014 9:58 pm

Thanks both of you! I could get the default skip durations to only 1 second and then send my own custom commands after that skip. So if I wanted to jump forwards 30 seconds, the user would press >|, WMC would jump 1 second (because I cannot block that event) but then my program would send 29 and >| to make it 30 seconds.

The logic is actually really simple. I've already written it using AHK a while back, but I'm going to rewrite it in VB.net because I wasn't a big fan of the language (it's really inconsistent), getting an MCE remote working in it needed the user to do some changes to the code and adding a GUI would be a pain in the backside.

If someone else wants to beat me to it, then I'm basically implementing a WMC version of Skip (by Jetson) for the Topfield. It's a smart advert skipper which means that the initial forward or backwards jumps are for a fixed length, until the direction of the jump is reversed. At that point, all subsequent jumps are halved each time until the user reaches the position they want.

As a result, you can jump any length of advert (or intro or credits or anything) by toggling >| and |< for a small number of presses.

crawfish

Posts: 465
Joined: Fri Jan 13, 2012 5:16 am
Location:

HTPC Specs: Show details

#5

Post by crawfish » Sun Oct 26, 2014 10:19 pm

You don't have to change the default skip intervals or send "double commands". Just intercept the relevant button press in AHK and send whatever command you want. What's the difficulty here?

Your smart skipper sounds interesting. I've been making do with the following (excerpted from my AHK file, commands only); they're bound to a row of four buttons on my Sony RM-VL610:

Code: Select all

send 30^+b ; Medium Step Back (30 sec)
send ^b ; Small Step Back (7 sec)
send 230^+f ; Commercial Small Step Forward (2.5 minutes)
send 400^+f ; Commercial Big Step Forward (4 minutes)

mrsilver

Posts: 63
Joined: Mon Oct 20, 2014 5:01 pm
Location:

HTPC Specs: Show details

#6

Post by mrsilver » Sun Oct 26, 2014 10:37 pm

Thanks for that, very useful.

One of the biggest issues I had with AHK 4 years ago when I originally wrote the code was that getting someones remote set up so AHKHID could actually see the button presses was a bit of a nightmare and involves some serious code editing. Not sure if that situation has changed.

Maybe it's changed?

crawfish

Posts: 465
Joined: Fri Jan 13, 2012 5:16 am
Location:

HTPC Specs: Show details

#7

Post by crawfish » Mon Oct 27, 2014 12:38 am

mrsilver wrote:Thanks for that, very useful.

One of the biggest issues I had with AHK 4 years ago when I originally wrote the code was that getting someones remote set up so AHKHID could actually see the button presses was a bit of a nightmare and involves some serious code editing. Not sure if that situation has changed.

Maybe it's changed?
You can modify ReportMappingTable to send ordinary key combinations, so you never had to deal with AHKHID. I briefly described what I do in this thread:

http://www.thegreenbutton.tv/forums/vie ... f=7&t=7555

Even more briefly, I remap all the remote buttons to send F13-F24 key combinations by modifying ReportMappingTable in the registry, and I have Autohotkey "modules" for WMC, XBMC, etc, that send the appropriate key combinations to those applications. You can catch some of the buttons directly, without modifying ReportMappingTable, but I described why I do what I do in the linked thread, which again, presented just a small subset of the full implementation to gauge interest.

One advantage to my approach I don't think I mentioned in the linked thread is that the remote has effect only in the programs I want it to control. You really don't want to modify ReportMappingTable to send Ctrl+S and similar combos; that's naive and dangerous, as the key combination will go to the program that has the focus. You need to use AHK as an intermediary, and since I have no programs that use F13-F24, my AHK script can eat those keys when a remote-enabled program doesn't have the focus.

Here are some more small excerpts relevant to the skip keys. This is what captures the skip forward and back buttons; remember, like all the buttons, they're remapped to F13-F24 key combinations with ReportMappingTable.

Code: Select all

#IfWinActive ahk_class eHome Render Window
F19:: ; [Skip Forward]
	ModWait()
	Send ^f
	return
F20:: ; [Skip Back]
	ModWait()
	Send ^b
	return
#IfWinActive
The above just sends the standard commands to WMC when WMC has the focus. Here are some other commands I've mapped to other buttons:

Code: Select all

#IfWinActive ahk_class eHome Render Window
^+F13:: ; cus1
	ModWait()
	Send 1000^+f ; Big Step Forward (10 minutes)
	return
^+F14:: ; cus2
	ModWait()
	Send 1000^+b ; Big Step Back (10 minutes)
	return
^+F16:: ; cus4
	ModWait()
	Send 30^+b ; Medium Step Back (30 sec)
	return
^+F17:: ; cus5
	ModWait()
	Send ^b ; Small Step Back (7 sec)
	return
^+F18:: ; cus6
	ModWait()
	send 230^+f ; Commercial Small Step Forward (2.5 minutes)
	return
^+F19:: ; cus7
	ModWait()
	send 400^+f ; Commercial Big Step Forward (4 minutes)
	return
#IfWinActive
The comments about cus1-cus12 refer to custom button codes I found to work, 0x70-0x7b. They don't ordinarily appear in ReportMappingTable, and they take some pressure off the small number of buttons Microsoft has defined. Unless you want to add a lot of buttons, you can probably get away with remapping predefined buttons you don't use, * and # being the easy ones, but there are also things like DVD Angle, Radio, Teletext, etc, as many as 15 or so depending on what you don't use. Most don't appear on my MCE remote, and to get them (and my cus1-cus12 buttons) into a universal remote, I use a programmable JP1 remote as an intermediary. The ModWait function was given and documented in the linked thread.

mrsilver

Posts: 63
Joined: Mon Oct 20, 2014 5:01 pm
Location:

HTPC Specs: Show details

#8

Post by mrsilver » Mon Oct 27, 2014 3:49 pm

Thanks for all the help. I've attached the code - although it's currently bound to O (back) and (P) forwards, so you'll probably want to change them.

Seems to work okay so far, although I struggled a lot with the AHK syntax and quirks - so it could probably be improved in places.

When you get used to it, you can jump adverts with only a couple of presses.

Code: Select all

;
; CONFIGURATION OPTIONS
;

; initialJump
; The number of seconds to jump first. This number will stay the same
; until you jump in the opposite direction in which case it'll reduce
; by half. The recommended amount is 300 seconds (5 minutes)

initialJump := 300

; timeOut
; The number of seconds between pressing buttons before the script
; considers this to be a new advert and resets the jumping time back
; the default (defined by "initialJump" above)

timeOut := 10

; You should not need to edit anything below here
; ------------------------------------------------------------------------

; Configuration of some settings
#SingleInstance force
#WinActivateForce
SetTitleMatchMode, 1
DetectHiddenText, On
SendMode Input

Initialise_Variables()
return

#IfWinActive ahk_class eHome Render Window
; Skip backwards
o::
   Handle_Button_Press(-1)
   return
   
; Skip forwards
p::
   Handle_Button_Press(1)
   return
#IfWinActive

; Initialise_Variables
; Reset all variables to initial defaults, used when the program first
; runs but also if you don't press a button for a short while (defined
; by 'timeOut' in the configuration).

Initialise_Variables()
{
	Global

	currentJump := initialJump  ; The amount of time to jump
	jumpModifier := 1  ; 1 = Jump time is unchanged, 0.5 = Jump time is halved
	jumpDirection := 0 ; 0 = Unknown, -1 = Backwards, 1 = Forwards
	lastJumpTime := 0  ; When the button was last pressed
}


; Handle_Button_Press
; Called when a button on the remote is pressed.
; newDirection is -1 if back was pressed, 1 if forwards was pressed

Handle_Button_Press(newDirection)
{
	Global

	; Wait for modifiers to be released
	KeyWait Shift
	KeyWait Ctrl
	KeyWait Alt
	KeyWait LWin
	KeyWait RWin
	
	; How long ago did we last press this button? If it's longer than
	; lastJumpTime then we should start jumping from the beginning
	; configuration (eg. re-Initialise).
	elapsedTime := A_TickCount - lastJumpTime
	if (elapsedTime > timeOut * 1000)
	{
		Initialise_Variables()
	}

	; Have we jumped before? If not, then we need to remember this
	; direction for subsequent jumps
	if (jumpDirection = 0)
	{
		jumpDirection = %newDirection%
	}
	else if (jumpDirection != newDirection)  ; We have swapped direction...
	{
		; ...so each jump should now be half of the previous
		jumpModifier := 0.5
	}
	
	; Work out how many seconds to jump and round up
	currentJump := Ceil(CurrentJump * jumpModifier)
	
	; If this is less than 2 seconds, stick to 2 seconds
	IfLess currentJump, 2, currentJump := 2

	; Remember what direction we are now going in
	jumpDirection = %newDirection%
	
	; We need to work out what to send to WMC, this is because you don't just send
	; the number of seconds, but minutes and seconds - so 55 seconds = press 55 but
	; 70 seconds = press 110 (1 min, 10 secs)
	theMins := Floor(currentJump/60)
	theSecs := "00" . Mod(currentJump, 60)

	StringRight, keyPress, theSecs, 2	
	If (theMins > 0)
	{
		keyPress = %theMins%%keyPress%
	}
		
	; Send keys to Windows Media Center
	if (newDirection = 1)
	{
		Send %keyPress%^+F
	}
	else
	{
		Send %keyPress%^+B
	}
 
	; Remember when we last jumped.
	lastJumpTime:= A_TickCount
}
My fear will be documenting how people configure it!

Let me know what you think...

mikinho

Posts: 248
Joined: Thu Jun 16, 2011 5:23 am
Location:

HTPC Specs: Show details

#9

Post by mikinho » Tue Oct 18, 2016 7:20 pm

If you don't want to mess with AHK or a service just write a Background MediaCenter AddIn to check the Remote and then call MediaTransport code.

mrsilver

Posts: 63
Joined: Mon Oct 20, 2014 5:01 pm
Location:

HTPC Specs: Show details

#10

Post by mrsilver » Mon Oct 31, 2016 8:19 pm

mikinho wrote:If you don't want to mess with AHK or a service just write a Background MediaCenter AddIn to check the Remote and then call MediaTransport code.
Good idea, I'll take a look because - quick frankly - AHK is such an utterly appalling language it makes me cry every time I use it. Everything is so inconsistent.

Having said that, since I started the effort, I've switched to Plex for non-WTV viewing so I probably need to see if I can get it working in that too.

Post Reply