Recent

Author Topic: Linking Keypresses to Arrays?  (Read 5817 times)

Isaac02444

  • Newbie
  • Posts: 1
Linking Keypresses to Arrays?
« on: January 28, 2013, 06:12:45 pm »
Hi there everyone! So, I've made an array for coins, like how you would put a coin in a slot and I was just wondering, how can I get the array to be done by a keypress function? This is what I have so far for the array:
-----------------------------------------------------------------------------------
program inputcash;

uses
crt,math,sysutils;

var
   total:integer
   input:array[1..9] of integer;

begin
input[1]:=1;
input[2]:=2;
input[3]:=5;
input[4]:=10;
input[5]:=20;
input[6]:=50;
input[7]:=100;
input[8]:=200;
input[9]:=0;
------------------------------------------------------------------------------

As you can see I've basically declared the array, now, each of the array values, like [1] is 1p, right?
How do I create it so that when I actually push the 1 button on my keyboard, that it adds the value of [1] into the total variable? Is there a specific code? Maybe something like...

Input:=keypress;
(further down the code)
total:=total+input;                 <<< May need another variable, opinions?

or something similar like that? Just so that I don't have to type '1' and hit enter for it to add one to the total?

Just to clarify if unsure, I want each button, so say for instance I pressed 5 on my keyboard.. Me physically pushing 5 on my keyboard, would then add the variable [5] into the total, if that makes sense?

I'm new to pascal coding, so please don't judge me because I have little understanding haha.

codeman

  • Jr. Member
  • **
  • Posts: 78
Re: Linking Keypresses to Arrays?
« Reply #1 on: January 28, 2013, 06:50:31 pm »
I just give you an example , you can modify it as you like ...

Add Windows  in uses ... on the top of your project.

Here is the main code...

Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
    i:=0;
    total:=0;     // INITIALIZATION
end; 


AND

Code: [Select]

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
     if (key=VK_1) then
     begin
         input[i]:=1;
         total+=input[i];
         showmessage(IntToStr(total));    // you can see the adding result each time you press a number
         i+=1;
         Exit;
     end;

     if (key=VK_2) then
     begin
         input[i]:=2;
         total+=input[i];
         showmessage(IntToStr(total));
         i+=1;
         Exit;
     end;

     if (key=VK_3) then
     begin
         input[i]:=3;
         total+=input[i];
         showmessage(IntToStr(total));
         i+=1;
         Exit;
     end;

     if (key=VK_4) then
     begin
         input[i]:=4;
         total+=input[i];
         showmessage(IntToStr(total));
         i+=1;
         Exit;
     end;
end;
« Last Edit: January 28, 2013, 06:53:27 pm by codeman »
Lazarus v2.02 Win10

TS-MPS

  • New Member
  • *
  • Posts: 35
Re: Linking Keypresses to Arrays?
« Reply #2 on: January 28, 2013, 07:29:52 pm »
You could either do this via the 'ord' or 'val' functions to get a simple method of doing this.

Basically you read a keypress (always read as a character, not a number otherwise your code could fall over if user enters a non-numeric).

The 'ord' function returns the ASCII code of the input, and because '0' has an ASCII code of 48, 1 is 49, 2 is 50 etc. then if we subtract 48 from the result we will in effect get the real number equivalent. This method works because the numbers run in sequence from 0-9. Obviously this method if no use once we go past 9 though, but if fine your your purposes as you only worry about single key press.

The other method is to use 'val' function which converts a character/string into a number, which then scales up to using any number values you want.

Here's how I would do it using 'ord':

Code: [Select]
program Project1;

uses
    crt,sysutils;
var
  total: integer ;
  coinvalues: array [1..9] of integer ;
  coinpress: char ;
  AllDone: boolean ;

begin
  coinvalues[1]:=1;
  coinvalues[2]:=2;
  coinvalues[3]:=5;
  coinvalues[4]:=10;
  coinvalues[5]:=20;
  coinvalues[6]:=50;
  coinvalues[7]:=100;
  coinvalues[8]:=200;
  coinvalues[9]:=0; // press the 9 key to exit

  AllDone := false ;
  total := 0 ; // don't forget to set it to zero before starting !

  repeat
        clrscr;
        Writeln ( 'Your coin total is: ' + inttostr ( total ) ) ;
        coinpress := readkey ; // read a key press as a character value
        if coinpress in [ '1'..'9' ] then // is it between '1' and '9' ?
        begin
          if coinpress = '9' then AllDone := true else
             total := total + coinvalues [ ord(coinpress)-48 ] ;
        end else
        begin
             // invalid key press, could display a message or beep here
        end;
  until AllDone = true ;
end.

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Linking Keypresses to Arrays?
« Reply #3 on: January 28, 2013, 07:46:49 pm »
Also anytime you are wanting to use keypress on a form
you need to set keypreview  to true in your forms properties
otherwise even though you have working code

It will not respond.


Also I would add  LCLType  to your uses


then you can call its properties windows by:

LCLType.  that is a period at the end of e  wait for properties to load

this will give you a better idea about virtual keys  IE  VK_0 - VK_9

it has other Virtual Key Consants as well but to get you somewhat on the right track
this method has aided me well when dealing with Keypress Events

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

Nebula

  • Jr. Member
  • **
  • Posts: 88
Re: Linking Keypresses to Arrays?
« Reply #4 on: January 28, 2013, 08:06:13 pm »
I knew nothing about this so it's caught my imagination.

I've just added  VK_ definitions link ( http://lazarus-ccr.sourceforge.net/docs/lcl/lcltype/index-2.html ) to the wiki page
http://wiki.lazarus.freepascal.org/LCL_Key_Handling

VK_1 is hex 31, or 49, and so on up to VK_9 at hex 39

But I think the OP isn't using forms/LCL, just the CRT kind of ReadKey/KeyPressed functionality  :)
Thanks for the hints and tips
Newbie testing Lazarus v1.0 - very impressed
Win 7 at work, XP and Linux Mint at home.
It all started with a ZX80 on a b/w telly........
Code: [Select]
Uses Smiles, GoodHumour, WantsToHelp;
{ never liked C - curly brackets are for comments! }

wjackson153

  • Sr. Member
  • ****
  • Posts: 267
Re: Linking Keypresses to Arrays?
« Reply #5 on: January 28, 2013, 08:12:06 pm »
Ok :)

Earlier he made a post simarly to this one but i can not find it now, perhaps was deleted
im not sure. but in his post he was using command buttons for coin slots. So i assumed
this was the same poster as before, and he was using a gui enviroment .

Perhaps there are two students with the same project, how ironic.

Lazarus 1.1 r39490 CT FPC 2.7.1 i386-linux KDE
Linux Mint 14 KDE 4

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Linking Keypresses to Arrays?
« Reply #6 on: January 29, 2013, 10:58:57 am »
Earlier he made a post simarly to this one but i can not find it now, perhaps was deleted
im not sure. but in his post he was using command buttons for coin slots. So i assumed
this was the same poster as before, and he was using a gui enviroment .
That's what I remember as well....

Perhaps there are two students with the same project, how ironic.
;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018