View Full Version : User Input from Keyboard
assoul
2004.11.01, 02:06 PM
Hi
I need to get user input from a keyboard for a carbon app that im building.
I cant figure out how to do that.
The documentation is sketchy and there r no code samples
can anybody help
ThemsAllTook
2004.11.01, 02:18 PM
You'll want to use the Carbon Event Manager for this. Documentation is available on developer.apple.com (http://developer.apple.com/).
Here's a quick example to get you started:
#include <Carbon/Carbon.h>
#include <stdio.h>
static OSStatus eventHandler(EventHandlerCallRef nextEvent, EventRef theEvent, void * userData) {
char charCode;
switch (GetEventClass(theEvent)) {
case kEventClassKeyboard:
switch (GetEventKind(theEvent)) {
case kEventRawKeyDown:
GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode);
printf("Key pressed: '%c'\n", charCode);
break;
case kEventRawKeyUp:
GetEventParameter(theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &charCode);
printf("Key released: '%c'\n", charCode);
break;
/* More event kinds here */
}
break;
/* More event classes here */
}
}
int main() {
EventTypeSpec eventTypes[] = {{kEventClassKeyboard, kEventRawKeyDown},
{kEventClassKeyboard, kEventRawKeyUp}};
InstallApplicationEventHandler(NewEventHandlerUPP( eventHandler), GetEventTypeCount(eventTypes), eventTypes, NULL, NULL);
RunApplicationEventLoop();
return 0;
}
Alex Diener
anonuser
2004.11.13, 07:44 PM
create a buffer if you want to test for multiple keys down.
look at kEventClassKeyboard for all the keyboard event classes.
FCCovett
2004.11.14, 12:03 AM
http://webpages.charter.net/utopiaplanetia/BasicCarbonStruct.dmg
assoul
2004.12.01, 06:44 PM
Hi
Could Somebody please post the sample code that was on here one more time...I had a few doubts and now the code segment has been removed by the moderator. So if you could pleasepost it again id be very grateful
assoul
2004.12.01, 07:08 PM
Hi
Im not sure how I can capture a whole sentence from the console and write it to a file when the user hits the return key...help!!!!
wadesworld
2004.12.01, 09:36 PM
Hi
Im not sure how I can capture a whole sentence from the console and write it to a file when the user hits the return key...help!!!!
Here's a super-simple example. Note that there's only basic error-checking.
#include <stdio.h>
int main()
{
char buf[201];
char *ret;
FILE *fp = NULL;
fp=fopen("outfile.txt", "w");
if (fp)
{
printf("Enter text (up to 200 characters) followed by return.\nOutput will be in outfile.txt.\n\nHit return on a line by itself to end.\n\n");
do
{
ret = fgets(buf, 201, stdin);
if ( ret )
fprintf( fp, "%s", buf );
} while ( *buf != '\n' );
}
if (fp)
fclose(fp);
}
Wade
assoul
2004.12.02, 06:03 PM
I have tried this earlier but it doesnt work...I need to take input from the standard C carbon console . but this method you have suggested doesnt work...on the other hand if I need to use the event handler class what part of the code actually takes the sentence that is the user input and stores it in a buffer?....and what kind of buffer do i need to create
i cant believe that a simple gets() doesnt work in carbon...ridiculous
Fenris
2004.12.02, 06:49 PM
Carbon isn't a console API, it's a GUI API. Thus, you can't have console input in Carbon, at least not the way you expect it. Try setting up a window with a text input field if you want to go that route. But, I have a creeping suspicion that you actually want a "Standard Tool" from the XCode/PB New Project menu.
wadesworld
2004.12.03, 04:27 PM
i cant believe that a simple gets() doesnt work in carbon...ridiculous
Like Fenris, I think you're confused. Carbon is an API. It includes functions for manipulating a Graphical User Interface.
If you want to accept user input, you have two basic options:
1) Write a graphical app. This requires text fields, event handlers and such.
2) Write a console, or terminal app. This is run from the command-line and does not contain any windowing of its own.
3) Use a "console window library" such as SOUIX from Codewarrior. This is a library that allow for console input, but does so in a console window.
Scenario #2 or #3 would be where you would use a gets() call, or fgets() in the example I posted.
Note that none of this differs from Windows for example. You can't just use gets() in a Windows application either - unless it's a command-line app.
Wade
assoul
2004.12.07, 03:53 PM
would that be the msl sioux ppc.lib?? or is there any other library i need to add
assoul
2004.12.07, 04:18 PM
when i add any sioux library i get a link error...cos the msl carbon d lib definitions r ignored cos of the sioux definitions and i cant get the program to run....so im still stuck....
and when u say console app r u referring to the standard c console debug out window that pops up when i run my program. and does sioux enable me to use fgets gets etc. from this window...im really in the dark here
assoul
2004.12.07, 04:45 PM
this is my code
#include <stdio.h>
#include <Gestalt.h>
#include <Resources.h>
#include <Devices.h>
#include <ToolUtils.h>
#include <conio.h>
#include <SoundInput.h>
#include <stdlib.h>
#include <String.h>
#include <QuickTime.h>
#include <TextUtils.h>
#include<Movies.h>
#include<Sound.h>
#include<Carbon.h>
#include<Time.h>
#include<unistd.h>
#include<console.h>
#include<CarbonEvents.h>
void soundPlay(char *);
void getstr(char *,short);
main()
{
FILE *fptr,*fpt,*fp;
SFTypeList typelist = {'TEXT'};
char filename[20],*str;
char str1[10],buf[50],*ret;
char fil[10],name[30],resp[50],c;
/*EventTypeSpec eventTypes[] = {{kEventClassKeyboard, kEventRawKeyDown},{kEventClassKeyboard,kEventRawKe yUp}};
*/
printf("Enter Your Name : ");
scanf("%s", &name);
printf("\nEnter name of file : ");
scanf("%s",&fil);
printf("\nPlease tyoe your response and hit return key for the next sentence : ");
fptr = fopen(fil,"r");
if(fptr == NULL)
printf("Error Can't open file");
else
{
while(fgets(filename,20,fptr) != NULL)
{
strcpy(str1,":");
str = strtok(filename,"\t");
while(1) {
str = strtok(NULL,"\t");
if(str == NULL)
break;
strcat(str1,str);
}
fpt = fopen("response.txt","a");
soundPlay(str1);
printf("\nEnter Response :");
gets(resp);
puts(resp);
}
}
/*getch();*/
return 0;
}
void soundPlay(char *str1)
{
FSSpec fileSysSpec;
SInt16 fileRefNum;
Str255 str2;
OSErr err;
Movie theSound;
c2pstrcpy(str2,str1);
err = FSMakeFSSpec(0,0L,str2,&fileSysSpec);
EnterMovies();
err = OpenMovieFile (&fileSysSpec, &fileRefNum, fsRdPerm);
if (noErr == err) {
err = NewMovieFromFile (&theSound, fileRefNum, 0, NULL,newMovieActive,NULL);
}
if (noErr == err) {
GoToBeginningOfMovie (theSound);
}
if (noErr == err) {
StartMovie (theSound);
}
if (noErr == err) {
while (!IsMovieDone (theSound)) {
MoviesTask (theSound, 0);
}
}
FSClose(fileRefNum);
}
as u can see after the sound file plays the user is expected to enter a response....i created a codewarrior project in standard c sioux waste carbon stationery
when i enter the response it should ideally display it again on the console but it doesnt happen
i tested using a hello world program and it works fine there as in after a gets the puts works
but in the app im building neither the gets works nor the puts....is there something else i am missing
please help
thanks
Fenris
2004.12.07, 06:18 PM
assoul, could you send me your project and code per email? I can't see anything wrong, but I'd like to see if I can find the problem by getting my hands on it. Mail me -> ivan [at] rusted [dot] se
assoul
2004.12.10, 04:16 PM
hi fenris
i emailed you the code....the mail subject is my project code or smthn like that :)
assoul
2004.12.13, 06:30 PM
can somebody tell me wats wrong with my code
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.