Hi, I`m new here and, of course, have a small problem.
I need to create a program with VC++ 6 Builder where:
Press ENTER once - open picture,
Press ENTER Twice - start a music.

I can`t figure out how to check if user press ENTER twice, can anybody help me?

My Code:

void __fastcall TForm1::ListBox1KeyPress(TObject *Sender, char &Key)
{
switch( Key )
  {
   case  'VK_RETURN':
   { atidarytpav(); }
   break;
}

Sry for my bad English

Keep a flag:

EnterKey = 0
if character = [ENTER_KEY} then EnterKey = EnterKey +1

You'll have to figure out what causes the flag EnterKey to be reset to 0.

Hi,

I am not an expert, so take my suggestions with a "grain of salt"!

First, having the the Enter Key being hit once or twice might be problematic given the nature of an event-driven program.

First lets address it from the perspective of if you just really want to stick with the once/twice Enter key approach, which I don't recommend. I would say maybe since the KeyPress is your triggering "event", try using that event procedure of ListBox1KeyPress to increment an int variable which will count up the number of times the enter key is pressed. In this way, when the ListBox1KeyPress runs, it can look at this int value. If the int counter is equal to 2, then it can both reset the counter int to zero, and then execute your picture viewing code. I can see some pitfalls with my own suggestion here, though.

There are other issues to address here, of course, such as what if the person clicks the Enter key once and then waits too long to click the second time? The more you look at the problems with using multiple hits of the Enter key, the more you will see the problems with it. Instead, you might want to consider using different keys for the different actions- this will greatly simplify your problem, since you will create a completely different 'case' to deal with for each one. This removes the ambiguity that using the same Key with different 'counts" would create.

Finally, you might create a different event handler entirely for different keys. I don't know how to do that in Builder, but I can post some code from MS Visual C++ .Net 2003 that works for creating separate event handlers for different keys if you like.

Hope this helps you some.

Jeff

Hi, I`m new here and, of course, have a small problem.
I need to create a program with VC++ 6 Builder where:
Press ENTER once - open picture,
Press ENTER Twice - start a music.

I can`t figure out how to check if user press ENTER twice, can anybody help me?

My Code:

void __fastcall TForm1::ListBox1KeyPress(TObject *Sender, char &Key)
{
switch( Key )
  {
   case  'VK_RETURN':
   { atidarytpav(); }
   break;
}

Sry for my bad English

Edited 5 Years Ago by jeff.reith: n/a

Yes I can use different letters and I used them early, however I`m cousing with problem, that my teacher preffered to use once, twice ENTER keypress.

WaltP >> Sry, but your code won`t work, now I`m feeling such noob... :(

Edited 5 Years Ago by alone882: n/a

Why not try something like this.
--1. user presses enter key.

--2. program executes a "wait" function. this function will wait for maybe 2 seconds.
-- maybe some sort of timer. the first keypress on the enter key start the timer
-- then sets a boolean value "withinTime" to true. after the timer stops, its sets
-- it back to false.

--3. if the user presses the enter key and "withinTime" is true - play the music.

--4. if the user presses the enter key and "withinTime" is false - show the picture.

This is just an idea. I think its similar to opening a folder - a single click selects it, a double click opens it. In the settings in windows there is actually a double clicking speed - so my idea is to have this "waiting time" until a new command is given.

*this is just an idea*

OK alone882, I understand why you are going with the Enter key twice, now. Darn teacher! :-)

So, create a new variable called "count_Enter" or something like that. Make it an integer, and initialize it with a value of zero. You will also want to add a timer to your form within your Builder IDE- this will be used to put a short time limit upon 1st click to 2nd click.

Within your key press event handler create code to evaluate count_Enter. If it is == 0, then you will want to increment it to 1 and start the timer. You will need to set a boolean variable (set by the timer), to indicate whether or not it is still running or not, as well.

So, if the user presses the Enter key once, but does not press it again within the timer length, then execute your once-Enter key code, and RE-set your count_Enter integer to zero. If during the timer running period (timer has NOT timed out yet) the user happens to hit the Enter key AGAIN, then execute the 2-Enter key code, then reset the count_Enter to zero.

I have not posted any code, just given you descriptions of what to do, but hopefully this is an idea that you can use to create some functioning code.

WaltP >> Sry, but your code won`t work, now I`m feeling such noob... :(

Maybe you did it wrong. As a noob, how can you tell it "won't work?" I've been using this technique for years. Maybe you need to describe what you don't understand about it.

I think I said something similar to what Slyte said- but Slyte said it better.

The key point is setting up some kind of wait/timer during which the user might press the Enter key a second time; if they do, then play music. If not, and the timer expires with just the first Enter key press having occurred, then you open the picture.

Ok I have this script

int pressCount = 0;
void __fastcall TForm1::ListBox1KeyPress(TObject *Sender, char &Key)
{
   
switch( Key )
  {
   case  'VK_RETURN':
   { 
      atidarytpav(); 
      pressCount = ++pressCount % 2 }
      if (count != 0)
      {
         //first time
      }
      else
      {
         //second time
         pressCount = 0;
      }

   break;
}

but now say

Undefined symbol 'count'

Do I need some library or what .. ?

HI alone882, no you don't need a library. Rather, it looks like you are inadvertently changing variable names within your code. The compiler is essentially tellin you that you are referencing "count" which is not something you have told it about yet. I *think* you meant to type: if (pressCount != 0) not if (count != 0) on that line.

Ok I have this script

int pressCount = 0;
void __fastcall TForm1::ListBox1KeyPress(TObject *Sender, char &Key)
{
   
switch( Key )
  {
   case  'VK_RETURN':
   { 
      atidarytpav(); 
      pressCount = ++pressCount % 2 }
      
      {
         //first time
      }
      else
      {
         //second time
         pressCount = 0;
      }

   break;
}

but now say

Undefined symbol 'count'

Do I need some library or what .. ?

Sry Guys, but this code wont work I try

switch( Key )
  {
   case  'VK_RETURN':
   { 
      pressCount = ++pressCount % 2 }
      if (pressCount != 0)
      {
         //first time
      }
      else
      {
         //second time
         pressCount = 0;
      }

   break;

And

int pressCount = 0;
switch( Key )
  {
   case 'VK_RETURN:
   {
      if (pressCount == 0)
      {
         //first time
         ++pressCount;
      }
      else
      {
         //second time
         pressCount = 0;
      }
    }
   break;

No one will work

Edited 5 Years Ago by alone882: n/a

That is great, alone882! Glad you got your problem solved.

Please mark this thread as "solved"- it might help someone trying to narrow down their own search for solutions withing the messages. Also, it would also be nice for you to post your C++ code that finally worked so that other noobs (like me) can learn from it as well. :-)

This article has been dead for over six months. Start a new discussion instead.