Try the program given above (inkey01.dbpro). If you're really lucky, you might display a character, but you'll have to be fast!

Similar documents
Screenshots Made Easy

Lesson 4 - Creating a Text Document Using WordPad

Create your first workbook

Contents. What's New. Version released. Newsletter #31 (May 24, 2008) What's New New version released, version 4.3.3

Lesson 4: Add ActionScript to Your Movie

Say you want a new blank document, just like the one Word shows you when you start the program. No problem here are the steps:

Excel Basics: Working with Spreadsheets

The new layer will be part of the template because we want it to appear alongside the nav bar on every page.

Microsoft Word 2011: Basic Tutorial

Chaos Culture. Multiclip Editor / Multiclip Note preview 1.5. Edited by Jason Cowling

Physics REU Unix Tutorial

COMP : Practical 6 Buttons and First Script Instructions

Designing and Printing Address Labels

MICROSOFT WORD 2010 BASICS

FrontPage. Directions & Reference

Using Visual Studio.NET: IntelliSense and Debugging

MAKING TABLES WITH WORD BASIC INSTRUCTIONS. Setting the Page Orientation. Inserting the Basic Table. Daily Schedule

Kindle Formatting Guide

Word: Print Address Labels Using Mail Merge

Chaos Culture. MIDI Modulators / Multiclip Note preview 1.6. Edited by Jason Cowling

Excel Tutorial Contents

Exploring extreme weather with Excel - The basics

DarkBASIC Pro: Counters and Timers Copyright 2011 A. Stewart 1

Organizing your Outlook Inbox

Lesson 4: Who Goes There?

Apple Modelling Tutorial

Vectorworks Essential Tutorial Manual by Jonathan Pickup. Sample

Shape Cluster Photo Written by Steve Patterson

The Fundamentals. Document Basics

Document Formatting with Word

Customizing Access Parameter Queries

Macromedia Director Tutorial 4

SIMPLE TEXT LAYOUT FOR COREL DRAW. When you start Corel Draw, you will see the following welcome screen.

Microsoft Excel 2013 Comments (Level 3)

Creating accessible forms

Introduction to Microsoft Word

COMPUTER TRAINING CENTER

Creating a Brochure, Flyer and Newsletter Using Microsoft Publisher 2003 for Windows 98/Me/2000/XP

Android Programming Family Fun Day using AppInventor

Online Business Account How to view and download reports

Class #7 Guidebook Page Expansion. By Ryan Stevenson

Manually Windows Update Vista Not Works In

Adobe Photoshop How to Use the Marquee Selection Tools

Lab 7 Macros, Modules, Data Access Pages and Internet Summary Macros: How to Create and Run Modules vs. Macros 1. Jumping to Internet

Tips & Tricks for Microsoft Word

Java Programming Constructs Java Programming 2 Lesson 1

Black Problem 2: Huffman Compression [75 points] Next, the Millisoft back story! Starter files

We created a few different effects and animations using this technique as applied to clones.

Step-by. A Very Warm Welcome to the Exciting World of Computers. Let s get Started It s easy with my Step- Instructions

Running Wordstar 6 on Windows 7 Using vdos

Grade: 7 Lesson name: Creating a School News Letter Microsoft Word 2007

Please click double click on the icon labeled Internet Explorer, the picture is a blue letter E.

Quick Guide. Choose It Maker 2. Overview/Introduction. ChooseIt!Maker2 is a motivating program at first because of the visual and musical

In this lesson you are going to create a drawing program similar to Windows Paint. 1. Start with a new project and remove the default cat sprite.

Using icloud's Mail rules to delete a message before you see it.

Teach Yourself Microsoft Word Topic 11 - Document Views and Review

Week 5 Creating a Calendar. About Tables. Making a Calendar From a Table Template. Week 5 Word 2010

Introduction. Basic Troubleshooting Tips. Computer Basics What are some troubleshooting techniques? What are Some Troubleshooting Techniques?

Address Bar. Application. The space provided on a web browser that shows the addresses of websites.

Fruit Snake SECTION 1

Detecting When the Mouse is Over a Sprite. Hidden sprite. Mouse pointer

Choosing Paper Size and Layout. UP TO SPEED Of Menus and Boxes

WORD PROCESSING FOR SALE. By Shel Silverstein

Eastside Literacy Tutor Support Word Processing Tutorial

Part 1 Simple Arithmetic

Other Loop Options EXAMPLE

Introduction to Publisher Tutorial & Videos

Use the Move tool to drag A around and see how the automatically constructed objects (like G or the perpendicular and parallel lines) are updated.

Basic Fiction Formatting for Smashwords in OpenOffice L. Leona Davis. Copyright 2012 L. Leona Davis All Rights Reserved

Text. In this Chapter: T Colouring Text. T Positioning Text. T Setting Text Visibility. T Sizing Text. T Determining Text Dimensions

WHCC Sports and Events

The Stack, Free Store, and Global Namespace

Microsoft Office Word 2010

Understanding Browsers

Essential Skills in ICT. Level 1. Guidebook

PowerPoint Slide Basics. Introduction

Using Excel to produce graphs - a quick introduction:

Bryce Lightning (Network Rendering)

Dreamweaver Website 1: Managing a Website with Dreamweaver

Earthwork 3D for Dummies Doing a digitized dirt takeoff calculation the swift and easy way

When using custom white balance the custom setting could be automatically deactivated under certain circumstances - this is no longer the case.

ST NICHOLAS COLLEGE RABAT MIDDLE SCHOOL HALF YEARLY EXAMINATIONS February 2016

How to create an eactivity on the Casio fx9860g

Creating a Brochure, Flyer and Newsletter Using Microsoft Publisher 2007 for

A cell is highlighted when a thick black border appears around it. Use TAB to move to the next cell to the LEFT. Use SHIFT-TAB to move to the RIGHT.

2. Getting Started When you start GeoGebra, you will see a version of the following window. 1

The Start menu (overview)

the NXT-G programming environment

Week 1 The Blender Interface and Basic Shapes

Unit Microsoft Word. Microsoft Word is the word processor included in Office. Word is one of the most popular word processors.

Civil Engineering Computation

Word Processing: Basic Skills

Debugging in Small Basic is the process of analysing a program to detect and fix errors or improve functionality in some way.

Lite. The 3D work organizer, concept-mapper, and study aid. Quickstart Guide (Topicscape Personal Edition Lite)

FrontPage Help Center. Topic: FrontPage Basics

Working with the Dope Sheet Editor to speed up animation and reverse time.

Heads Up Design: Focusing on the drawing area

Teach Yourself Microsoft Word Topic 11 - Document Views and Review

A Document Created By Lisa Diner Table of Contents Western Quebec School Board October, 2007

Transcription:

Keyboard Input Introduction Although most games don't need to accept text from the keyboard, there will be occassions when this would be useful (perhaps entering your name for a high score). The INPUT statement isn't really up to the job of keyboard input if we want our program to stay in sprite or 3D mode. So, in these next couple of pages we'll have a look at another approach to keyboard input. The INKEY$ Statement The INPUT waits for you to type in as many characters as you want, displaying each on the screen, and stops only once you've pressed Enter. INKEY$, on the other hand, is quite a different type of statement. It accepts only one character, the character doesn't get displayed on the screen, and it doesn't even wait for you to hit the key! If you're not pressing a key at the instant the INKEY$ statement is executed, then you've missed it! If the INKEY$ statement executes when no key is being pressed, it returns an empty string; if a key is being pressed, then a string containing that key is returned. LISTING-Sup001 Trying to use INKEY$ The program in LISTING-Sup001is a rather optimistic attempt at reading a key and displaying its value. REM *** Read a key *** REM *** Display it *** PRINT ch$ REM *** End program *** WAIT MOUSE Activity Sup004 Try the program given above (inkey01.dbpro). If you're really lucky, you might display a character, but you'll have to be fast! We could give ourselves a better chance if we put the INKEY$ statement in a loop structure (see LISTING_Sup002). LISTING-Sup002 Waiting for INKEY$ REM *** Keep trying until a key press is detected *** REM *** Display the key *** PRINT ch$ REM *** End program *** WAIT MOUSE Hands On DarkBASIC Pro (Supplemental) 9

Activity Sup005 Try this version of the program (you should have more success this time). Reading More than One Key Since we're likely to need to type something a little longer, we need some way of reading more than a single key. Perhaps if we put the code already produced in a loop (see LISTING-Sup003). LISTING-Sup003 Displaying the Keys Entered Notice the semicolon at the end of the PRINT statement. This keeps all the output on the same line. DO LOOP Activity Sup006 Try out this latest version. Any surprises? The trouble is that the computer can execute the loops many times in a fraction of a second, so the INKEY$ statement checks the keyboard several times before you can get your finger off the keyboard. One way round this would be to add in a delay to slow everything down: DO WAIT 200 LOOP LISTING-Sup004 Waiting for the Key Press to Stop but this method doesn't work too well since fast typists will be annoyed by the slow speed while slower people are still likely to get repeating keys. A better approach is to wait until no keys are being pressed before starting round the loop again (see LISTING-Sup004). DO REM *** Wait until no keys are pressed *** WHILE INKEY$() <> "" WHILE LOOP Activity Sup007 Try out this new version. 10 Hands On DarkBASIC Pro (Supplemental)

In fact, this code will be so useful, it's worth creating a function from it which reads a single character from the keyboard: FUNCTION ReadAKey$() WHILE INKEY$() <>"" WHILE FUNCTION ch$ Notice that the routine doesn't try to display the character read; instead it returns the character. That way anyone using the routine can do whatever they want with the character, rather than be forced to have it appear on the screen. Activity Sup008 Create the function given above and use it in a program which accepts and displays characters entered, stopping when a question mark is entered. The logic required is Read a character Display the character UNTIL the character is a? Activity Sup009 The trouble with the last program is that the? itself is displayed on the screen. Modify your program so that the? character does not appear. What happens if you press the Enter key? Not all keys produce a printable character. For example, in the last Activity we saw that the Enter key appears to have no effect. In fact every key causes some character to be returned. We can prove this by displaying the ASCII code of the character read rather than the character itself. This requires the line: PRINT ASC(ReadAKey$()); Activity Sup010 Modify your last program to display the ASCII code of each character entered. What is the ASCII code for the Enter and Backspace characters? Change the program back to displaying the actual characters entered, rather than their ASCII code. Modify your program so that the WHILE..WHILE loop terminates when the Enter key is pressed rather than with the question mark. Hands On DarkBASIC Pro (Supplemental) 11

Creating a Replacement for INPUT The INPUT statement has four characteristics that we need to include in any routine that reads a string from the keyboard: Each character entered also appears on the screen Entry is complete when the Enter key is pressed The backspace character can be used to delete previously entered characters. The complete set of characters entered is returned as a string which can be asigned to a variable. We'll have to add each of these characteristics to any new routine. We already know how to stop input when the Enter key is pressed. LISTING-Sup005 Text Output in Sprite/3D Mode Getting each character to appear on the screen may seem to be a done deal too, but if we use our new routine along with sprites or 3D objects, any text we place on the screen will disappear every time the screen is refreshed (see LISTING-Sup005). REM *** Use manual screen updating *** ON REM *** Create a 3D object - that way we use a different mode *** MAKE OBJECT CUBE 1,1 REM *** Update the screen *** : REM *** Display char and update screen *** WHILE FUNCTION ReadAKey$() WHILE INKEY$() <>"" WHILE FUNCTION ch$ Activity Sup011 Change your code to match the program in LISTING-Sup005. Notice that only one character at a time is displayed. To get round this problem, we can add each character entered to a string and display that string. The updated main section of the program is shown below: ON MAKE OBJECT CUBE 1,1 : 12 Hands On DarkBASIC Pro (Supplemental)

SET CURSOR 100,100 WHILE Activity Sup012 Modify your program to match the code above. We're almost there now. All that is left to do is delete a character from the string when the backspace is pressed. The basic logic for this is: IF keypressed is the backspace THEN Remove the last character from the result$ string Add character to result$ IF Display result$ However, we should only delete from the string if it contains at least one character, so, the IF statement's logic needs to be: IF keypressed is the backspace AND result$ contains at least one characterthen... Activity Sup013 Update your program to match the logic above. HINTS : Use LEN to find out the number of characters in result$. Use LEFT$ to delete the last character from result$. Now we're ready to create a function from our code: FUNCTION ReadAString$(x,y) IF x < 0 OR y < 0 EXITFUNCTION "" IF result$ = LEFT$(result$,LEN(result$)-1) IF WHILE FUNCTION result$ Notice that a pre-condition check has been added for the lower values of x and y but none for the upper values. This is because the upper limit depends on the screen resolution, but we could have included some sort of upper limit just to stop ridiculous values being accepted. Hands On DarkBASIC Pro (Supplemental) 13

By using a box sprite and placing the output at the correct position, we could create the illusion of a text input box as shown in FIG-Sup004. FIG-Sup004 Entry within an Edit Box The complete program for this is given in LISTING-Sup006. LISTING-Sup006 Implementing Keyboard Entry within a Edit Box We need to tell the program to draw the sprite first otherwise the text ends up under the edit box! REM *** Set up screen *** SET DISPLAY MODE 1280,1024,32 REM *** Use manual screen updating *** ON REM *** Make sure sprite appears before text *** DRAW SPRITES FIRST REM *** Load the edit box sprite *** LOAD IMAGE "editbox.bmp",1 SPRITE 1,300,300,1 REM *** Update screen *** : REM *** Change text style and colour *** SET TEXT FONT "Arial Black" SET TEXT SIZE 20 INK 0,0 REM *** Read the string *** name$ = ReadAString$(320,320) REM *** End program *** FUNCTION ReadAString$(x,y) IF x < 0 OR y < 0 EXITFUNCTION "" IF result$ = LEFT$(result$,LEN(result$)-1) IF WHILE FUNCTION result$ FUNCTION ReadAKey$() WHILE INKEY$() <>"" WHILE FUNCTION ch$ 14 Hands On DarkBASIC Pro (Supplemental)

Activity Sup014 Modify your program to match the code above. (Make sure you've copied editbox.bmp into your folder). What happens if you enter about 35 characters? Since it doesn't look too good if we allow the text to extend outside the text box, we could modify the ReadAString$() function to specify a maximum number of characters that can be entered. The new code is highlighted: FUNCTION ReadAString$(max,x,y) IF x < 0 OR y < 0 OR max < 1 EXITFUNCTION IF result$ = LEFT$(result$,LEN(result$)-1) IF LEN(result$) < max IF IF WHILE FUNCTION result$ The font being used in the example, Arial Black, is a proportional font (that is, some letter are wider than others - w is wider than i), so when you're setting an upper limit for the number of characters, test your code with wide letters - such as W - to check that everything fits within the text box. Activity Sup015 Modify your program to match the code above and find out the upper limit for max when using the sprite image shown. The only thing missing from the input is a cursor, and although we can't create a flashing one, we could add the illusion of a cursor by displaying the " " character (ASCII code 124) at the end of the entered string, changing the line to +CHR$(124) and to get the "cursor" to show up at the start we'll also need to add the lines: PRINT CHR$(124) near the start of the routine. Hands On DarkBASIC Pro (Supplemental) 15

So, at last, we have a final version of our replacement for the INPUT statement: FUNCTION ReadAString$(max,x,y) IF x < 0 OR y < 0 OR max < 1 EXITFUNCTION "" IF PRINT CHR$(124) result$ = LEFT$(result$,LEN(result$)-1) IF +CHR$(124) WHILE FUNCTION result$ Activity Sup016 Change your code to match that given above and test out your program. 16 Hands On DarkBASIC Pro (Supplemental)

Solutions Activity Sup004 To stop when the Enter key is pressed we have to check for the key's ASCII code being equal to 13. The main section now changes to: Activity Sup005 Activity Sup006 Even the quickest key press seems to generate about 12 copies of the character! Activity Sup007 Activity Sup008 UNTIL ch$="?" FUNCTION ReadAKey$() WHILE INKEY$() <>"" WHILE FUNCTION ch$ Activity Sup009 The easiest way to stop the? appearing on the screen is probably to replace the..until loop in the main section with a WHILE..WHILE structure. The main section now becomes: WHILE ch$ <> "?" WHILE Activity Sup010 To display the ASCII code for each character pressed the main section PRINT statement should be changed as shown below: WHILE ch$ <> "?" PRINT ASC(ch$)," "; WHILE The Enter character has a ASCII code of 13 while the backspace character has the code 8. WHILE Activity Sup011 Activity Sup012 Activity Sup013 The code for the main section is now: ON MAKE OBJECT CUBE 1,1 : result$ = LEFT$(result$, ÄLEN(result$)-1) IF SET CURSOR 100,100 WHILE Activity Sup014 If you enter too many characters the text overflows the edit box. Activity Sup015 About 23 is the upper limit before the characters start moving outiside the box. Activity Sup016 Hands On DarkBASIC Pro (Supplemental) 17