Discussion:
How to simulate key press events
Abhijeet Singh
2011-04-20 07:00:23 UTC
Permalink
Hi,I have a table view in my application. One of the cell of the table view is editable. When my application window appears on screen the first row is by default added in table view and the cell is in editable mode. On Enter or Esc key press the user input from the cell is accepted and the data of whole row is saved to some other data structure. I capture the key depressions in following method (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandWhat i am trying to do now is when user try to click on some other row while the current row is in edit mode I want to ask him that the "current record is not saved yet . do you want to save it?". If he says yes then i want to save it. For that I need to simulate the Esc key press so that my routine on Esc key press gets called.And if user says NO then i just want to discard the current row.I tried to simulate Esc key press when table view selection is changing in following code but it do
esn't simulates the Esc or any other key press." (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command"never gets called. (void)tableViewSelectionIsChanging:(NSNotification *)aNotification{int item1, item2;int rc;//Check items in wlarray is equal to items in worklistdata array item1 = [wlarray count];for(item2 = 0; item2 < MAXSAMPLES; item2++){if(worklistdata[item2].sampleid[0] == '\0')break;}if(item1 > item2){rc = NSRunAlertPanel(@"Current worklist item is not saved yet. Do you want to save it?", @"",@"Yes",@"No", nil);if(rc == NSAlertDefaultReturn){CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);CGEventRef saveCommandDown = CGEventCreateKeyboardEvent(source, (CGKeyCode)53, YES);//53 is keycode for Esc CGEventSetFlags(saveCommandDown, kCGEventFlagMaskCommand);CGEventRef saveCommandUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)53, NO);CGEventPost(kCGAnnotatedSessionEventTap, saveCommand
Down);CGEventPost(kCGAnnotatedSessionEventTap, saveCommandUp);CFRelease(saveCommandUp);CFRelease(saveCommandDown);CFRelease(source);}}}Please help.Thanks & RegardsAbhijeetDear macosxdev ! Get Yourself a cool, short @in.com Email ID now!
Christiaan Hofman
2011-04-20 09:03:32 UTC
Permalink
Post by Abhijeet Singh
Hi,I have a table view in my application. One of the cell of the table view is editable. When my application window appears on screen the first row is by default added in table view and the cell is in editable mode. On Enter or Esc key press the user input from the cell is accepted and the data of whole row is saved to some other data structure. I capture the key depressions in following method (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandWhat i am trying to do now is when user try to click on some other row while the current row is in edit mode I want to ask him that the "current record is not saved yet . do you want to save it?". If he says yes then i want to save it. For that I need to simulate the Esc key press so that my routine on Esc key press gets called.And if user says NO then i just want to discard the current row.I tried to simulate Esc key press when table view selection is changing in following code but it do
First of all: why do you use Esc for committing an edit? That is wrong, Esc is for ending/canceling something, not for editing.

As for simulating a key press event, you can create an NSEvent instance and post it ([NSApp postEvent:atStart:]).

However in general you should not do this, I would strongly advice against it in this case.

I think you should better look into other delegate methods, such as controlTextDidEndEditing:. Using control:textView:doCommandBySelector: seems to be the wrong method to use for saving an edit, cocoa objects usually have many other ways in which editing can end (such as changing focus, or through universal access). Generally, you should program on the *function* that is performed, not on the *way* in which it is done.

Oh, and PLEASE add newlines between paragraphs (the only newline is in a completely wrong place), your email, especially the code, is totally unreadable.

Christiaan
Abhijeet Singh
2011-04-20 10:16:30 UTC
Permalink
Hi,You are right. I should have used controlTextDidEndEditing and such methods to handle editing inside table view. But unfortunately my client/customer wants that whenever window appears the keyboard focus should be inside editable field of the table view row and on Enter key press the current record (data from the row of table view) should be saved and a new row should be added to the view (again focus will be inside editable cell). I have done that but the only problem is when user does not press the Enter key and tries to click on some other row of table view or some other control of the window I just want to ask/prompt user that the current item is not saved? Can you tell me the event that fires whenever a focus is shifted from one control (table view) to some other control so that i will show my messagebox on that event. Following doesnt work:tableview: SelectionIsChanging It works only when table view row selection changes. If user clicks a button on the window this m
ethod is not called.controlTextDidEndEditing Same as aboveselectionShouldChangeInTableView same as aboveThanks & RegardsAbhijeet Original message From:"Christiaan Hofman"< ***@gmail.com >Date: 20 Apr 11 14:33:43Subject: Re: How to simulate key press eventsTo: Cc: "macosxdev" On Apr 20, 2011, at 9:00, Abhijeet Singh wrote:> Hi,I have a table view in my application. One of the cell of the table view is editable. When my application window appears on screen the first row is by default added in table view and the cell is in editable mode. On Enter or Esc key press the user input from the cell is accepted and the data of whole row is saved to some other data structure. I capture the key depressions in following method (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandWhat i am trying to do now is when user try to click on some other row while the current row is in edit mode I want to ask him that the "current record is not s
aved yet . do you want to save it?". If he says yes then i want to save it. For that I need to simulate the Esc key press so that my routine on Esc key press gets called.And if user says NO then i just want to discard the current row.I tried to simulate Esc key press when table view selection is changing in following code but it do> esn't simulates the Esc or any other key press." (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)command"never gets called. (void)tableViewSelectionIsChanging:(NSNotification *)aNotification{int item1, item2;int rc;//Check items in wlarray is equal to items in worklistdata array item1 = [wlarray count];for(item2 = 0; item2 < MAXSAMPLES; item2++){if(worklistdata[item2].sampleid[0] == '\0')break;}if(item1 > item2){rc = NSRunAlertPanel(@"Current worklist item is not saved yet. Do you want to save it?", @"",@"Yes",@"No", nil);if(rc == NSAlertDefaultReturn){CGEventSourceRef source = CGEventSourceCreate(kCGEv
entSourceStateCombinedSessionState);CGEventRef saveCommandDown = CGEventCreateKeyboardEvent(source, (CGKeyCode)53, YES);//53 is keycode for Esc CGEventSetFlags(saveCommandDown, kCGEventFlagMaskCommand);CGEventRef saveCommandUp = CGEventCreateKeyboardEvent(source, (CGKeyCode)53, NO);CGEventPost(kCGAnnotatedSessionEventTap, saveCommand> Down);CGEventPost(kCGAnnotatedSessionEventTap, saveCommandUp);CFRelease(saveCommandUp);CFRelease(saveCommandDown);CFRelease(source);}}}Please help.Thanks & RegardsAbhijeetDear macosxdev ! Get Yourself a cool, short @in.com Email ID now!First of all: why do you use Esc for committing an edit? That is wrong, Esc is for ending/canceling something, not for editing. As for simulating a key press event, you can create an NSEvent instance and post it ([NSApp postEvent:atStart:]). However in general you should not do this, I would strongly advice against it in this case.I think you should better look into other delegate methods, such as controlTextDidE
ndEditing:. Using control:textView:doCommandBySelector: seems to be the wrong method to use for saving an edit, cocoa objects usually have many other ways in which editing can end (such as changing focus, or through universal access). Generally, you should program on the *function* that is performed, not on the *way* in which it is done.Oh, and PLEASE add newlines between paragraphs (the only newline is in a completely wrong place), your email, especially the code, is totally unreadable.ChristiaanGet Yourself a cool, short @in.com Email ID now!
Christiaan Hofman
2011-04-20 10:55:42 UTC
Permalink
Post by Abhijeet Singh
Hi,
You are right. I should have used controlTextDidEndEditing and such methods to handle editing inside table view. But unfortunately my client/customer wants that whenever window appears the keyboard focus should be inside editable field of the table view row and on Enter key press the current record (data from the row of table view) should be saved and a new row should be added to the view (again focus will be inside editable cell). I have done that but the only problem is when user does not press the Enter key and tries to click on some other row of table view or some other control of the window I just want to ask/prompt user that the current item is not saved?
Can you tell me the event that fires whenever a focus is shifted from one control (table view) to some other control so that i will show my messagebox on that event.
tableview: Se lectionIsChanging - It works only when table view row selection changes. If user clicks a button on the window this method is not called.
controlTextDidEndEditing - Same as above
If you started an edit, than this method should be called. It is called for me every time.

Otherwise, you may also override resignFirstResponder, but better not.
Post by Abhijeet Singh
selectionShouldChangeInTableView - same as above
Thanks & Regards
Abhijeet
Generally, I'd use controlTextDidEndEditing, and decide what to do based on the NSTextMovement userInfo from the notification.

Christiaan
Post by Abhijeet Singh
---------- Original message ----------
Date: 20 Ap r 11 14:33:43
Subject: Re: How to simulate key press events
Hi,I have a table view in my application. One of the cell of the table view is editable. When my application window appears on screen the first row is by default added in table view and the cell is in editable mode. On Enter or Esc key press the user input from the cell is accepted and the data of whole row is saved to some other data structure. I capture the key depressions in following method (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandWhat i am trying to do now is when user try to click on some other row while the current row is in edit mode I want to ask him that the "current record is not saved yet . do you want to save it?". If he says yes then i want to save it. For that I need to simulate the Esc key press so that my rout ine on Esc key press gets called.And if user says NO then i just want to discard the current row.I tried to simulate Esc key press when table view selection is changing in following code but it do
First of all: why do you use Esc for committing an edit? That is wrong, Esc is for ending/canceling something, not for editing.
As for simulating a key press event, you can create an NSEvent instance and post it ([NSApp postEvent:atStart:]).
However in general you should not do this, I would strongly advice against it in this case.
I think you should better look into other delegate methods, such as controlTextDidEndEditing:. Using control:textView:doCommandBySelector: seems to be the wrong met hod to use for saving an edit, cocoa objects usually have many other ways in which editing can end (such as changing focus, or through universal access). Generally, you should program on the *function* that is performed, not on the *way* in which it is done.
Oh, and PLEASE add newlines between paragraphs (the only newline is in a completely wrong place), your email, especially the code, is totally unreadable.
Christiaan
Loading...