FAQFAQ   SearchSearch   UsergroupsUsergroups   RegisterRegister   ProfileProfile    Log inLog in   RSS Feed

Tutorial: A very simple script sample
Goto page 1, 2, 3, 4, 5  Next
 
Post new topic   Reply to topic    Salling Software Forums Forum Index -> Scripting Salling Clicker for Windows
View previous topic :: View next topic  
Author Message
salling
Site Admin


Joined: 27 Jul 2004
Posts: 7498
Location: Stockholm, Sweden

PostPosted: Tue Oct 18, 2005 12:11 pm    Post subject: Tutorial: A very simple script sample Reply with quote

As promised, here's a mini-tutorial in how to create a simple keypad-based script for Salling Clicker. Note that there's a couple of things that can be improved in this script, but I wanted to keep the first example as brief as possible.

[b]This article assumes you're interested in building script plugins for Salling Clicker and that you have some experience in working with JScript or some other scripting language for Windows.[/b]

I have chosen to not make this script available as a simple download, as it is merely an example. As is, it's not very useful, practical, or representative of what can be done with Salling Clicker.

[b]The keystroke simulation APIs[/b]

To send a "character", use:

[code:1]SendUnicodeKeystroke( unicode value );[/code:1]

To simulate the pressing of a specific key:

[code:1]SendVirtualKeystroke( virtual keycode value, shiftState, ctrlState, altState, winState );[/code:1]
(the "state" parameters are booleans)


[b]The anatomy of a script package[/b]

Every Salling Clicker script is packaged in a directory containing at least one script file for the implementation and one XML file describing the script. Our sample script will be packaged like so:

\KeypadSample\KeypadSample.xml
\KeypadSample\KeypadSample.js

I used JScript because I like it better than VBScript, but VBScript would work equally well if you're more comfortable with this scripting language.

Start by creating the directory "KeypadSample" on your desktop. Then create two text files inside it "KeypadSample.xml" and "KeypadSample.js"

[b]The package description file[/b]

Now, let's fill those files with some meaning. Starting with the XML file, here's what it should contain:

[code:1]
<?xml version="1.0" encoding="utf-8"?>

<ClickerScript>
<Path>KeypadSample.js</Path>
<Author>Salling Software AB</Author>
<Category>Samples</Category>
<Description></Description>
<UUID>D0189DF0-CF32-4018-9F0F-6EF36B7F9677</UUID>
<Title>Keypad Sample</Title>
<Type>ClickerItem</Type>
<URL>http://clicker.salling.com</URL>
</ClickerScript>
[/code:1]

This tells Salling Clicker who wrote the script, what its title is, and whether it's a "Clicker Item" or "Phone Event" script, among other things. Of particular importance are the UUID and Path elements. These give your script a unique identifier and also point to the script file that we shall soon implement.

[b]Important: [/b] as you make your own scripts, make sure each script has its own unique ID. You can use this web page to make new UUIDs: http://kruithof.xs4all.nl/uuid/uuidgen

[b]A simple script implementation[/b]

Our KeypadSample.js script file is current empty. That's no good! Let's make it do something.

What I had in mind is exceptionally simple. When the user selects the "Keypad Sample" item on his or her device, I'd like to show a screen with some instructional text, and then I want to capture keypad keystrokes to generate hopefully useful Windows keystrokes.

In this sample, I'm going to generate keystrokes that may (or may not) be useful for controlling a media center. Please note that I do not have access to one of these, so I'm walking in the dark here. Please bear with me. Also note that one should be able to do way cooler things by integrating with the COM object model in MCE instead of generating keystrokes like in this simple sample.

Well, enough with the caveats, and on to the script! Open your file "KeypadSample.js" in a text editor and paste the following:

[code:1]
var widget = CreateKeypadScreen( "mykeypad_" );
widget.title = "Keypad Sample";
widget.CreateRow( "Demo", scCenter, scClip, scLarge );
widget.CreateRow( "Use the d-pad", scCenter, scWrap, scMedium );
theTerminal.Push( widget );
[/code:1]

Now what the heck does this do!? Well, first it creates a "keypad screen" widget (there are many other types of widgets you can create). Then, we create some text rows to show the informational text I promised. Finally, the new widget is pushed to "the terminal"; that is, it is made active/visible in response to the user selecting our wonderful new clicker item on the handheld device.

Note: theTerminal is the name of an object supplied to the script by Salling Clicker. It represents the device and has a number of properties you can query for information about the device (size of the display, its name, and some other things). For now, all we need to care about is the Push() method.

What about handling keystrokes then? Well, the "mykeypad_" string value passed to the widget constructor tells the widget how to call back into the script in response to events.

There is a couple of events we might want to handle, but in this example all we care about are "key down" events. We add a handler for these by appending this to our script:

[code:1]
function mykeypad_KeyDown(theScreen, theKey)
{
if( theKey == "s" ) {
// VK_MEDIA_PLAY_PAUSE
SendVirtualKeystroke( 0xB3, false, false, false, false );
} else if( theKey == "v" ) {
// VK_DOWN
SendVirtualKeystroke( 0x28, false, false, false, false );
} else if( theKey == "^" ) {
// VK_UP
SendVirtualKeystroke( 0x26, false, false, false, false );
} else if( theKey == "<" ) {
// VK_LEFT
SendVirtualKeystroke( 0x25, false, false, false, false );
} else if( theKey == ">" ) {
// VK_RIGHT
SendVirtualKeystroke( 0x27, false, false, false, false );
} else if( theKey == "u" ) {
// VK_VOLUME_UP
SendVirtualKeystroke( 0xAF, false, false, false, false );
} else if( theKey == "d" ) {
// VK_VOLUME_DOWN
SendVirtualKeystroke( 0xAE, false, false, false, false );
} else if( theKey == "c" ) {
// VK_VOLUME_MUTE
SendVirtualKeystroke( 0xAD, false, false, false, false );
}

// Keep the keypad active
return true;
}
[/code:1]

That should be it!

mykeypad_KeyDown() is called by Salling Clicker whenever a key is pressed on the device. The argument "theKey" contains a string value with a code representing the key pressed. SendKeystroke() is a method in Salling Clicker that generates a "synthetic" keyboard event inside Windows. The first parameter is the virtual keycode for the event we want to generate.


Now. Save the files and drag the package directory into My Documents\My Clicker Scripts. Open Salling Clicker -> Clicker Items, select the "Sample" script category, and drag the "Keypad Sample" script over into the Clicker Items area to the left. Click Apply and try it from your device.

I gave this script a shot in WMP 10, and pressing the d-pad center on my device made it play/pause, as expected. Pressing the C-key (I tried on a Nokia 3650 which has one of these keys) toggled mute. Again, as expected.

[b]Epilogue[/b]

This script is very naive in that it does not attempt to direct the keystrokes to a specific application. Moreover, it does not check to see if such an application is actually launched. We can deal with those things later.


I hope you found this sample useful!

Cheers.
--
Jonas

PS. All the scripts included with Salling Clicker are available for you to inspect. Open them from the script browser inside Salling Clicker, or find them in the Salling Clicker program directory. Please don't make changes to these; instead make a copy, change the UUID, and put the copy in "My Clicker Scripts". This will avoid issues when Salling Clicker is upgraded.


Last edited by salling on Mon Jul 23, 2007 11:34 am; edited 3 times in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
teetow
Senior Member


Joined: 11 Oct 2005
Posts: 72
Location: Uppsala, Sweden

PostPosted: Tue Oct 18, 2005 2:23 pm    Post subject: Reply with quote

Jonas, you are an absolute godsend.

This is exactly what I needed to get started, and combined with some clever reverse-engineering of the default scripts I'm sure I can cook up what's necessary to create a rudimentary Media Player Classic control. A thousand thanks.

I have a fuzzy idea about COM (Common Object Model, right?) objects, and I've stumbled across it occasionally when clawing through the net for info on just how Windows applications can communicate with each other. Anything you might be able to give us on this would be very, very helpful aswell - even if it's just an URL to a tutorial.

For now, though, this sample is probably enough to get us started.

One last thing; the virtual keycodes. I googled around and found [url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp]this.[/url] Is it the same thing?
Back to top
View user's profile Send private message
salling
Site Admin


Joined: 27 Jul 2004
Posts: 7498
Location: Stockholm, Sweden

PostPosted: Tue Oct 18, 2005 2:35 pm    Post subject: Reply with quote

Yep, that page lists the codes you can use.

Longer-term, I really think you should also look into using COM/ActiveScripting. Search google for "scripting" and "WMP8".

Best.
--
Jonas
Back to top
View user's profile Send private message Send e-mail Visit poster's website
teetow
Senior Member


Joined: 11 Oct 2005
Posts: 72
Location: Uppsala, Sweden

PostPosted: Tue Oct 18, 2005 2:54 pm    Post subject: Reply with quote

Yeah, I'll absolutely do that. Doesn't that put some sort of requirement on the target application, though? Or is it something assumed when coding for Windows, pretty much like the Applescript "tell application play" thing on MacOS X?

Oh, and maybe I should clarify. Media Player Classic is not a Windows Media Player version, it's an independent app (URL [url=http://sourceforge.net/projects/guliverkli/]here[/url]). Kinda along the lines of VLC or MPlayer.

Also, this SendKeystroke thing. How do I do keyboard modifiers, e.g. Ctrl-LeftArrow? I took a wild swing at the second parameter (currently "false" for all the lines in the example) and messed around with it, but I got nowhere.
Back to top
View user's profile Send private message
Cheruby
Newbie


Joined: 18 Oct 2005
Posts: 1
Location: Germany - Aachen

PostPosted: Wed Oct 19, 2005 12:36 am    Post subject: Reply with quote

Hi,

thank you for the starting point.

I really like this piece of software and bought it right away after I saw it in action with ITunes.

At the moment I try to gather ideas what to control remotely, many of them seem nice for about five minutes, but are more or less of no use.

Started to wrap around the freeware NirCmd to turn off the monitor etc. and changed the Shutdown script to work as a sleep timer. But for now that is all I need besides the standard scripts.

As soon as a few good ideas come to my mind I will be back here asking questions :-)

Maybe I will try to communicate with python in some way (as I am more or less a python-programmer) Would be interesting to evaluate the possibilities using python and SC on both series60 and windows.

If there is any help / information I can give you concerning most of the newer Nokia phones I would be happy to share them as far as I am allowed to do.


BR,

Dominique
Back to top
View user's profile Send private message
Apollo
Junior Member


Joined: 18 Oct 2005
Posts: 43

PostPosted: Wed Oct 19, 2005 2:48 am    Post subject: Reply with quote

Jonas you rule! This is exactly the start I needed. I have worked with Javascript before for websites so this wasn't as scary as it would seem. I am working on a script now exclusively for media center that can hopefully gather feedback from media center as well to show on your palm what file is playing and such. Would you happen to know any coding resources for Media center scripting? I don't mean to be baby-fed but I have looked around alot and I keep getting run in circles and can't find a specific recource for codes.

My other question is in relation to the hex bits in your JS file. For each of these lines:

[quote]
// VK_VOLUME_MUTE
SendKeystroke( 0xAD, false );
[/quote]

What do I need to change the hex values to? IS there a list somewhere that lists these hex codes or are they not important?

Thanks!!

~*Apollo*~
Back to top
View user's profile Send private message
Apollo
Junior Member


Joined: 18 Oct 2005
Posts: 43

PostPosted: Wed Oct 19, 2005 3:07 am    Post subject: Reply with quote

Ok another big question for you! What are all of the palm keys? I can see that there is S, <, >, etc. What is all of them?

Thanks :)
Back to top
View user's profile Send private message
teetow
Senior Member


Joined: 11 Oct 2005
Posts: 72
Location: Uppsala, Sweden

PostPosted: Wed Oct 19, 2005 9:02 am    Post subject: Reply with quote

[quote="Apollo"]What do I need to change the hex values to? IS there a list somewhere that lists these hex codes or are they not important?[/quote]
Hey, fellow scripters! Glad to see the party started =)

As mentioned earlier, there's a list of virtual keycodes [url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp]here[/url].
Back to top
View user's profile Send private message
Apollo
Junior Member


Joined: 18 Oct 2005
Posts: 43

PostPosted: Wed Oct 19, 2005 9:14 am    Post subject: Reply with quote

What are the palm script keys? So for instance, how can I bind a volume up command to the Calender button on the palm?
Back to top
View user's profile Send private message
salling
Site Admin


Joined: 27 Jul 2004
Posts: 7498
Location: Stockholm, Sweden

PostPosted: Wed Oct 19, 2005 12:59 pm    Post subject: Reply with quote

Hi!

Try this to learn what codes are associated with the keys:

[code:1]
function mykeypad_KeyDown(theScreen, theKey)
{
ShowMessage( theKey );
return true;
}
[/code:1]

Cheers.
--
Jonas

[quote="Apollo"]What are the palm script keys? So for instance, how can I bind a volume up command to the Calender button on the palm?[/quote]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Apollo
Junior Member


Joined: 18 Oct 2005
Posts: 43

PostPosted: Wed Oct 19, 2005 6:51 pm    Post subject: Reply with quote

I don't understand what that does. And what I mean is what are the palm hardkey script names? So when I press calender or contacts, what keys should I be putting in the script so those are used?

For instance, 's' seems to be center press on the palm. And '<' seems to be left 5-way nav button. So do you know what they all are? The code you gave me doesn't seem to do anything but make my script buggy (Keypad script you started us on) What am I doing wrong?
Back to top
View user's profile Send private message
salling
Site Admin


Joined: 27 Jul 2004
Posts: 7498
Location: Stockholm, Sweden

PostPosted: Wed Oct 19, 2005 7:29 pm    Post subject: Reply with quote

Hi Apollo!

I'm not giving the codes, because I don't know what model Palm you have. The new snippet I gave you should display on the PC the code for each button you press.

Try starting with the original script, and just stick this statement into the top of the keydown handler method:

[code:1]
ShowMessage( theKey );
[/code:1]

I hope this helps.

Best.
--
Jonas
Back to top
View user's profile Send private message Send e-mail Visit poster's website
roychang
Newbie


Joined: 15 Oct 2005
Posts: 16

PostPosted: Wed Oct 19, 2005 7:38 pm    Post subject: Reply with quote

Hi, how do I do keyboard modifiers like Ctrl+T and so on like what teetow asked earlier?
Back to top
View user's profile Send private message
Apollo
Junior Member


Joined: 18 Oct 2005
Posts: 43

PostPosted: Wed Oct 19, 2005 7:54 pm    Post subject: Reply with quote

I bet if you look at some of the other scripts that come packaged with salling clicker that you can find your answer ;)
Back to top
View user's profile Send private message
roychang
Newbie


Joined: 15 Oct 2005
Posts: 16

PostPosted: Wed Oct 19, 2005 8:12 pm    Post subject: Reply with quote

[quote="Apollo"]I bet if you look at some of the other scripts that come packaged with salling clicker that you can find your answer ;)[/quote]

As a matter of fact none of the default scripts even used the SendKeystroke() function, only the example in this thread revealed this function. If you know, please share it here.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Salling Software Forums Forum Index -> Scripting Salling Clicker for Windows All times are GMT + 2 Hours
Goto page 1, 2, 3, 4, 5  Next
Page 1 of 5

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group