Sign in to follow this  
Followers 0
lod3n

HTPC Text Input via Remote Demo

1 post in this topic

Here's a quick and dirty example on how to handle input from the number pad found on many Home Theater PC's remotes. It's far from perfect, but it gets the job done, and is easy to understand. It's not ready for prime time, but you could use it as a foundation for something personally useful.

#include <GUIConstants.au3>
#Include <String.au3>

Global $sKeys = ObjCreate("scripting.dictionary")
populateKeymap()

$gui = GUICreate("Simple HTPC Remote Typer", 633, 454, 193, 115)

$Output = GUICtrlCreateEdit("", 8, 112, 609, 329)
GUICtrlSetFont(-1, 32, 400, 0, "MS Sans Serif")

$Input = GUICtrlCreateInput("", 8, 8, 265, 54)
$hInput = GUICtrlGetHandle($Input)
GUICtrlSetFont(-1, 32, 400, 0, "MS Sans Serif")

$Preview = GUICtrlCreateLabel("", 312, 8, 268, 60)
GUICtrlSetFont(-1, 32, 400, 0, "MS Sans Serif")

$Label1 = GUICtrlCreateLabel("=", 280, 16, 27, 50)
GUICtrlSetFont(-1, 32, 400, 0, "MS Sans Serif")

$Label2 = GUICtrlCreateLabel("* = Accept Letter      ** = Done      # = Backspace", 8, 72, 424, 29)
GUICtrlSetFont(-1, 16, 400, 0, "MS Sans Serif")

GUISetState(@SW_SHOW)

ControlFocus ($gui, "", $hInput )
$lastinput = ""
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    
    $sInput = GUICtrlRead($input)
    
    If $sInput = "*" Then
        $sOutput = stripGTLT( GUICtrlRead($Output) )
        
        ; this is to show how it works, you'd want to comment this out for real use
        MsgBox(0,"Simple HTPC Remote Typer OUTPUT",$sOutput)
        
        ; uncomment the 2 lines below to use this for real
        ;GUIDelete($gui)
        ;Send($sOutput) ; sends the text to your htpc app
        
        Exit
    EndIf
    
    
    ;backspace
    If StringInStr($sInput,"#") Then
        If $sInput = "#" Then
            $sOutput = stripGTLT( GUICtrlRead($Output) )
            $sOutput = ">" & StringTrimRight($sOutput,1) & "<"
    
            GUICtrlSetData($input,"")
            GUICtrlSetData($Preview,"")
            GUICtrlSetData($Output,$sOutput)
            ControlFocus ($gui, "", $hInput )
            
        Else        
            $sInput = StringReplace($sInput,"#","")
            $sInput = StringTrimRight($sInput,1)
            GUICtrlSetData($input,$sInput)
        EndIf
    EndIf
    
    ;place letter in output box
    If StringInStr($sInput,"*") Then
        $sPreview = stripGTLT( GUICtrlRead($Preview) )
        
        $sOutput = stripGTLT( GUICtrlRead($Output) )
        
        GUICtrlSetData($input,"")
        GUICtrlSetData($Preview,"")
        GUICtrlSetData($Output,">" & $sOutput & $sPreview & "<")
        ControlFocus ($gui, "", $hInput )
    Else    
        If $sInput <> $lastinput Then
            $lastinput = $sInput ; only change stuff when input changes - prevents flicker
            If StringInStr($sInput,"1") And $sInput <> "11" Then
                $capsmode = True
                $sInput = StringReplace($sInput,"1","")
            Else
                $capsmode = False
            EndIf
            
            if $sKeys.exists($sInput) Then
                $key = $sKeys.item($sInput)
                If $capsmode Then $key = _StringProper($key)
                GUICtrlSetData($Preview,">"&$key&"<")
            EndIf
        EndIf
    EndIf
    
WEnd

Func stripGTLT($string)
    $string = StringReplace($string,">","")
    $string = StringReplace($string,"<","")
    Return $string
EndFunc
    
    
Func populateKeymap()
    ; feel free to add more key mappings as you see fit
    $sKeys.add("11","1")
    $sKeys.add("2","a")
    $sKeys.add("22","b")
    $sKeys.add("222","c")
    $sKeys.add("2222","2")
    $sKeys.add("3","d")
    $sKeys.add("33","e")
    $sKeys.add("333","f")
    $sKeys.add("3333","3")
    $sKeys.add("4","g")
    $sKeys.add("44","h")
    $sKeys.add("444","i")
    $sKeys.add("4444","4")
    $sKeys.add("5","j")
    $sKeys.add("55","k")
    $sKeys.add("555","l")
    $sKeys.add("5555","5")
    $sKeys.add("6","m")
    $sKeys.add("66","n")
    $sKeys.add("666","o")
    $sKeys.add("6666","6")
    $sKeys.add("7","p")
    $sKeys.add("77","q")
    $sKeys.add("777","r")
    $sKeys.add("7777","7")
    $sKeys.add("77777","s")
    $sKeys.add("8","t")
    $sKeys.add("88","u")
    $sKeys.add("888","v")
    $sKeys.add("8888","8")
    $sKeys.add("9","w")
    $sKeys.add("99","x")
    $sKeys.add("999","y")
    $sKeys.add("9999","z")
    $sKeys.add("99999","9")
    $sKeys.add("0"," ")
    $sKeys.add("00","0")
EndFunc

post-14785-1191463005_thumb.png


[font="Fixedsys"][list][*]All of my AutoIt Example Scripts[*]http://saneasylum.com[/list][/font]

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0