Flex Custom Cursor Tutorial

Skill

Flex Custom Cursor Tutorial

Posted in:

On our site here we are kinda obsessed with showing how to use your own cursors in everything - and so now its time for a tutorial on how to do the basic cursor manipulation in flex. In this tutorial you will learn how to replace the cursor with your own custom image. There are some more complex techniques that can be used for cursor management in Flex but I am going to hold these off for a later tutorial, as most people probably won't need them.

Below you can check out the demonstration of the application we are going to build today. The application is very simple - it simply has two buttons. The first button changes the cursor to my super sweet custom cursor and the second button changes the cursor back to normal. Ok, now take a few seconds and be amazed by my cursor creation genus. The source code for the tutorial.

Get Adobe Flash player

To start off we are going to build the simple interface that is in the application. This will not include the little button for the source. Ok so there is a panel and two buttons, very simple. Check out the code below.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
   width="350" height="200" layout="absolute">
  <mx:Panel title="Having Fun with Cursors" x="0" y="0"
     width="100%" height="100%" layout="absolute">
    <mx:Button label="Show Cursor" x="10" y="59" width="115"/>
    <mx:Button label="Remove Cursor" x="205" y="59" width="115"/>
  </mx:Panel>

Next up is the the code to show the the cursor and remove them. The first thing is setting up the click event listener for both buttons. I have named these functions showCursor and removeCursor. The updated code is below.

<mx:Button label="Show Cursor" click="showCursor()" x="10" y="59" width="115"/>
<mx:Button label="Remove Cursor" click="removeCursor()" x="205" y="59" width="115"/>

Now we get to the actionscript code. We add a script tag and the functions for the cursors:

<mx:Script>
 <![CDATA[
   import mx.managers.CursorManagerPriority;
   import mx.managers.CursorManager;
 
   [Embed("/cursors/cc.png")]
   private var customCursor:Class;
 
   private function showCursor():void
   {
     CursorManager.setCursor(customCursor, CursorManagerPriority.HIGH, 3, 2);
   }
 
   private function removeCursor():void
   {
     CursorManager.removeAllCursors();
   }
 ]]>
</mx:Script>

To get the cursor to show up we need to do two things. The first is to create a private variable that holds the cursor which is of type Class. It is here that we embed the image we want to use. This can be any of several image types including jpg, png, swf, and more. In the showCursor function we use the CursorManager to set the cursor, as seen above. The parameters we pass are first the cursor, next is the priority (this can always be high for simple cases), the last two are the offset for the hot spot of the pointer in x and y. So basically where the actual pixel is that should be used for the pointer. And that is it for showing a cursor.

Removing the cursor is just as easy. You simply need to call CursorManager.removeAllCursors() to remove any custom cursors. There are some more complex ways to remove individual cursors but you will have to wait for a later tutorial for this.

Well I know it was short but I hope that you learned something useful from it. And look for a more complex tutorial explaining some of the other intricacies of the CursorManager class. As always drop a comment if you have any issues with the code or have any questions at all.

Amar Shukla
06/09/2008 - 00:04

nice work :) ..thnx..its usefull for me like novice

reply

Ganesh
07/24/2008 - 03:20

Good one. Very easy to understand. Many thanks.

reply

Katy
08/05/2008 - 06:11

Is it possible to use this to remove the white circle with the red cross that appears when a user is dragging an item that is not over a drop target? If it is could you tell me where to place it as I've been playing around with it but can't seem to remove it. Thanks

reply

Mariosh
09/08/2008 - 12:29

Great tutorial. Thanks

reply

Noel
09/10/2008 - 10:39

an issue with this and the CursorManager in general is that when you press the button the default cursor appears on the screen with the custom cursor and only disappears after a mouse move event...
can't seem to find a workaround for it...

reply

The Fattest
10/10/2008 - 10:53

This example doesn't work like that noel, the cursor will appear as soon as the click event is completed and removed the same. No movement of the mouse is needed.

reply

GT
09/12/2008 - 14:00

Nice job. The thing I'm struggling with at the moment is getting the cursor in a chart to display as a crosshair - just a vertical line 100% height, and a horizontal line 100% width, intersecting at the cursor coordinates. (It's an absolute doddle in Flash, but I can't get it happening in Flex).

I've got a sense that your tutorial just gave me a pointer (ha ha).

Cheers

GT
France (but I'm an Aussie... hence the bad English)

reply

Ravi
10/11/2008 - 18:33

Hi,
I am also looking around for a cross-hair, move and hand cursor to use with my flex application. I got some PNG files for these. But the problem is that these files have a white background and when we use this cursor in the app, it show up with that white background !!!

Also, for the cross hair cusor, the pointer's co-ordinates are the left top corner of that PNG image - not at the center of the cross-hair !!

Any suggestions ?

reply

Robert R
12/16/2008 - 02:01

Just try the setCursor method with negative offsets. It works surprisingly well.
Have fun
Robert

reply

Sunny
04/07/2009 - 21:08

Hi, if there are some default cursor provided as the cross-hair, move cursor by adobe so i don't need to create one by myself.
So all i need to do is to use
[Embed(source="...",symbol="...")]
can i?

reply

Tibi
01/20/2009 - 04:49

when using this code in Air app I'm having symptoms similar with Noel ( after removing cursor the mouse disappear and only show after a mouse move ).

The same code compiled as a Flex application is working perfect. Do you have any idea is that is a bug or do you know a workaround ?

Thank you

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors