Recent

Author Topic: How to check for blank entry (enter key)  (Read 4291 times)

nugax

  • Full Member
  • ***
  • Posts: 232
How to check for blank entry (enter key)
« on: January 22, 2018, 04:48:32 am »
I have an if statement checking for #10 for an enter key pressed , but it doesn't seem to work. Any ideas?
-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: How to check for blank entry (enter key)
« Reply #1 on: January 22, 2018, 04:52:25 am »
I said #10 I meant I am checking for #13
-Nugax

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: How to check for blank entry (enter key)
« Reply #2 on: January 22, 2018, 05:03:14 am »
Please show us the code.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to check for blank entry (enter key)
« Reply #3 on: January 22, 2018, 05:56:21 am »
as far as I remember about readln, it does not return the enter key to the variable read from the keyboard, you need to clean the keyboard buffer after each readln to clean up the ln portion of the read operation. You simply check for empty string usually after a trim operation to avoid being tricked from a space or control characters.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: How to check for blank entry (enter key)
« Reply #4 on: January 22, 2018, 06:51:33 am »
@nugax

To do like what taazz said, try something like this:

Code: Pascal  [Select][+][-]
  1. if S = '' then ... // do something

or
Code: Pascal  [Select][+][-]
  1. if S.Trim = '' then ... // do something

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: How to check for blank entry (enter key)
« Reply #5 on: January 22, 2018, 08:07:27 am »
or even simpler:
Code: Pascal  [Select][+][-]
  1. if s.IsEmpty then //
Note a space can be valid input, so depending on application you may want to simply test for length(s)=0, which is what IsEmpty does.

In a Lazarus GUI application it is much simpler: edit controls listen to VK_RETURN, eg:
Code: Pascal  [Select][+][-]
  1. // make sure lcltype is in the uses clause!!
  2. procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  3. begin
  4.   if Key = VK_RETURN then Caption := 'Enter/Return pressed';
  5. end;

Note this is cross-platform too...
Console apps have an inferred LineEding when using ReadLn, like:
Code: Pascal  [Select][+][-]
  1. program keytest;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$I-}{$endif}
  3. uses sysutils;
  4. var
  5.   s:string;
  6. begin
  7.   writeln('Press somekeys or not, then press enter');
  8.   readln(s);  // LineEnding is always inferred when using ReadLn...
  9.   if s.IsEmpty then writeln('Empty') else writeln('NotEmpty: ',s);
  10. end.
« Last Edit: January 22, 2018, 09:49:47 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

nugax

  • Full Member
  • ***
  • Posts: 232
Re: How to check for blank entry (enter key)
« Reply #6 on: January 22, 2018, 04:24:14 pm »
@nugax

To do like what taazz said, try something like this:

Code: Pascal  [Select][+][-]
  1. if S = '' then ... // do something

or
Code: Pascal  [Select][+][-]
  1. if S.Trim = '' then ... // do something


Is S a string? I do not have a trim option on a string[255];
-Nugax

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: How to check for blank entry (enter key)
« Reply #7 on: January 22, 2018, 04:30:55 pm »
Trim and IsEmpty and other String Helper functions  are provided by unit SysUtils, so you need to add SysUtils into your uses clause.

https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.html
« Last Edit: January 22, 2018, 04:32:34 pm by Handoko »

nugax

  • Full Member
  • ***
  • Posts: 232
Re: How to check for blank entry (enter key)
« Reply #8 on: January 22, 2018, 05:00:13 pm »
Trim and IsEmpty and other String Helper functions  are provided by unit SysUtils, so you need to add SysUtils into your uses clause.

https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.html


I have sysutils, but for some reason Trim doesnt work.
-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: How to check for blank entry (enter key)
« Reply #9 on: January 22, 2018, 05:31:14 pm »
Trim and IsEmpty and other String Helper functions  are provided by unit SysUtils, so you need to add SysUtils into your uses clause.

https://www.freepascal.org/docs-html/rtl/sysutils/tstringhelper.html


I have sysutils, but for some reason Trim doesnt work.


I figured it out! Thanks!
-Nugax

 

TinyPortal © 2005-2018