Advertisement
  1. Code
  2. JavaScript

Quick Tip: Detect User Inactivity in a Flash Movie

Scroll to top
Read Time: 3 min

In this Quick Tip, you'll learn how to detect when the user has been inactive for a determined time. Keep reading to find out how!


Final Result Preview

Let's take a look at the final result we will be working towards:


Step 1: Brief Overview

We'll detect users interaction using Keyboard and Mouse Events, checking for recent activity using a Timer. We'll display a message if the determined time has elapsed without activity.


Step 2: Set Up Your Flash File

Launch Flash and create a new Flash Document.

Set the stage size to 550x250px.


Step 3: Interface

This is the interface we'll be using, a simple background with a message telling the user to wait for five seconds. The following screen will be shown when the five seconds have passed - convert it to MovieClip and mark the Export for ActionScript box. It's named TheScreen.

Of course this is only one of the many things you can do when the inactivity time passes, GrooveShark for example, pauses the music and shows a message asking the user if he wants to continue using the application. It's your call to decide what to do.


Step 4: ActionScript

This is the class that does the work, read the comments in the code to find out about its behavior.

1
2
package 
3
{
4
	import flash.display.Sprite;
5
	import flash.events.TimerEvent;
6
	import flash.utils.Timer;
7
	import flash.events.MouseEvent;
8
	import flash.events.KeyboardEvent;
9
10
	public class Main extends Sprite
11
	{
12
13
		private var screen:TheScreen = new TheScreen(); //Creates a new instance of TheScreen

14
		private var added:Boolean = false; //A boolean to check if the screen has been added to stage

15
16
		/* Timer Object */
17
18
		private var timer:Timer = new Timer(5000);//Five seconds for this example

19
20
		public function Main():void
21
		{
22
			timer.start(); //Starts the timer

23
			timer.addEventListener(TimerEvent.TIMER, showMsg); //Listens for the timer to complete

24
25
			/* Mouse and keyboard listeners, stops the timer when a event occurs, if you are using other input method, like the microphone, add its event here */
26
27
			stage.addEventListener(MouseEvent.MOUSE_MOVE, stopTimer);
28
			stage.addEventListener(MouseEvent.MOUSE_DOWN, stopTimer);
29
			stage.addEventListener(MouseEvent.MOUSE_UP, stopTimer);
30
31
			stage.addEventListener(KeyboardEvent.KEY_DOWN, stopTimerK);
32
			stage.addEventListener(KeyboardEvent.KEY_UP, stopTimerK);
33
		}
34
35
		/* If there is no activity for 5 seconds, a message will display */
36
37
		private function showMsg(e:TimerEvent):void
38
		{
39
			addChild(screen); //Adds the screen

40
			added = true;
41
		}
42
43
		/* If there's activity, we clear the message and reset the timer */
44
45
		private function stopTimer(e:MouseEvent):void
46
		{
47
			if (added)
48
			{
49
				removeChild(screen);
50
				added = false;
51
			}
52
53
			timer.stop();
54
			timer.start();
55
		}
56
57
		private function stopTimerK(e:KeyboardEvent):void
58
		{
59
			if (added)
60
			{
61
				removeChild(screen);
62
				added = false;
63
			}
64
65
			timer.stop();
66
			timer.start();
67
		}
68
	}
69
}

Step 5: Document Class

Remember to add the class name to the Class field in the Publish section of the Properties panel.


Conclusion

This is a useful and easy way to detect user activity. In this example the activity detected is based in the Mouse and Keyboard events, but you can easily add a Microphone or other input event to meet your needs.

I hope you liked this Quick Tip, thanks for reading!

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.