Re: How to simulate key press events

Christiaan Hofman <[email protected]> Wed, 20 Apr 2011 11:03:32 +0200
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
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 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!

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