|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#46
|
||||
|
||||
|
grr, i tried my mess of a program out today, and it didn't work. there seems to have been a problem with writing to a file. i' not sure why, but that seems to be where the error was from. there was no syntax errors, so there was something wrong somewhere else. could i actually need two threads? in that case, i think i know what to do, i just need someone to help me on what method i need to take.
|
|
#47
|
||||||
|
||||||
|
I'd be happy to help. Just send your source code my way, and I'll help with any parts you need. ICQ: 152894206 (file transfers probably won't work, but just attach them to a message here, and I'll take a look.)
|
|
#48
|
|||||
|
|||||
|
How to monitor the keyboard without blocking on every cycle? Try in_avail()
Quote:
Check out in_avail. That may do what you want. I believe if you call cin.in_avail() it will return > 0 if a key has been struck. You can simply avoid doing the keyboard read if there's nothing to read. |
|
#49
|
|||
|
|||
|
keybd_event
I wrote 2 apps that talked to eachother (just for fun). One app used kb_event() to put keyboard events out for the other to pick up with getch(). Basically, do keybd_event(VK_(a letter or function),0,0,0); Have the recieving proggie active, use getch and look at the char it returns. IE to send the letter A to the other progg have app 1 go keybd_event(VK_A,0,0,0). The other is waiting with a myrecievingchar = getch(); I wrote a header file to include the letters and numbers (( windows.h has all the kb_event crap, but only defines the keys that arent useful)) I can get that posted if you want it, but it is really just a bunch of #define VK_A 65, #define VK_B...
Oh, and getch is in conio.h Hope that helped, but i am guessing it didnt. Mayhaps the unique data sending approach was vaguely interesting though |
|
#50
|
||||
|
||||
|
hmm, i tried the in_avail thing, and it doesn't work. heck, it's not even recognized by my compiler (VC++). as for the rest of my code, i don't use ICQ, so the number you gave me means nothing to me
. if you want, i do have AIM (yeah, i hate AOL too). |
|
#51
|
|||||
|
|||||
|
Quote:
|
|
#52
|
||||
|
||||
|
hmm, that syntax works, but how would i run that in a while loop? like, what number does 'cin.rdbuf( )->in_avail( )' equal when no key is pressed? i'm not sure of that.
|
|
#53
|
||||
|
||||
|
ok, i've figured out the 'cin.rdbuf( )->in_avail( )' line seems to always equal 0, no matter what i do. how do i change this?
|
|
#54
|
||||||
|
||||||
|
after you've done the cin.rdbuf( )->in_avail( ), you must actually read in a byte. a simple 'cin>>myChar' should do nicely. the problem is, in_avail() on checks to see if data is there, it doesn't clear it from the buffer. Therefore, that same data will be there the next time and you will always get 0 back. If you're already doing this, post the offending code and I'll take a look at it.
|
|
#55
|
||||
|
||||
|
is there anyway for the computer to skip the cin>>myChar if no data is entered? cause otherwise, the loop will stop once every cycle for me to hit enter. that's no good. could i use the 'keybd_event(i,0,0,0);' event to hit enter, if no data is entered? my whole problem hinges around the fact that i know no way of skipping a cin without putting in data. any help?
|
|
#56
|
||||||
|
||||||
|
I think I've got the solution...
Going back to a previous post (by gwross), your best method will be to use kbdhit(). It will return 0 if no key has been pressed, and 1 if a key has been pressed. Since I assume you only want it to quit when a certain key is pressed, you can then issue a getch() which will return the key that was pressed. Just compare it, and you should be good to go. Just make sure to include <conio.h>.
Here's the code I tested it with: while(true){ if(kbdhit()){ cout<<"key pressed:"; myChar=getch(); cout<<myChar<<"\n"; if(myChar=='q') return 0; } else{ cout<<"no key pressed\n"; } cout.sync_with_stdio() } BTW, that cout.sync_with_stdio() is because I was having problems with the output being very sluggish. |
|
#57
|
||||
|
||||
|
Quote:
Last edited by Dave Flowerday : 08-06-2002 at 17:49. |
|
#58
|
|||||
|
|||||
|
Quote:
BTW, for readability (probably not efficiency) you could save the return value of cin.rdbuf() in a variable (say cinBuffer) -- outside the loop and then only call cinBuffer->in_avail() inside the loop. |
|
#59
|
||||||
|
||||||
|
I've been playing around with this too, and I've run into the same problems. It seems that the only way to get data to cin so that in_avail() will notify you is to do a cin! For example, if you call cin<<x, but at the prompt type something like "5 10 15", then in_avail() will return the number of chars that are still in the buffer. In this case, either 6 or 7 (I can't remember which), because the first one has been eaten by your cin statement. As far as I can tell, this would be useful in figuring out how much data a user input, but only after the user actually input the data.
Or I could be completely wrong... Anyway, the code I posted above seems to work (based on your earlier suggestion). If anyone can get this working with cin, I'd be interested to hear how. |
|
#60
|
||||
|
||||
|
well, i tried the _getch () and _getche () functions (from conio.h). just a simple loop to test it, and it worked pefectly, doing exactly what i wanted. from what i understand, it reads the I/O stream, and you don't even have to hit enter. it will just go on and on until there is input on the keyboard, i think. if that's the case, i just need to identify a certain key, and all should work as i want.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Win32 serial port - recieve buffer | seanwitte | Programming | 4 | 05-11-2003 21:41 |
| White Paper Discuss: FIRST Editor v1.2 | CD47-Bot | Extra Discussion | 1 | 18-09-2003 19:32 |
| How to communicate both through radio and serial port? | JC Denton | Programming | 1 | 08-02-2003 23:43 |
| Ever seen this serial port problem before??? | Aidan F. Browne | Programming | 5 | 05-02-2003 12:03 |
| serial communication using c++ | Malakili | Programming | 31 | 01-05-2002 15:51 |