Hi everyone, I'm just starting out in Cocoa development, and having some trouble making a basic calculator application. Basically, I have an NSTextField in which the user types an equation they would like solved.
I really want to intercept/remap keystrokes made by the user into the NSTextField. So if the user types a 'u' character, I want it to come out as a '4' a la a virtual numeric keypad (if that makes sense). I basically want to remap some keys. I also want to intercept some other keystrokes such as the arrow keys.
For the arrow keys, I've managed to implement the control:TextViewoCommandBySelector, which seems to work fine (even though I don't quite know what's going on). But for the keys with actual character values, I just can't seem to get it to work.
After a lot of googling, I think I'm supposed to use
- (BOOL)textViewNSTextView *)textView shouldChangeTextInRangeNSRange)affectedCharRange replacementStringNSString *)replacementString
delegate method, but this doesn't work for me. In this case, I don't even know what the replacementString and affectedCharRange variables represent. Do I just ignore them? I'm not sure this is even what I want though - I just want a simple key remap.
Plus, this function appears to apply to an NSTextView, not an NSTextField - I think there's some overlap between these two classes, but it's all just serving to confuse me. I've also played around with other methods, such as insertText, but I don't quite know what I'm doing exactly.
Is it just me or is Apple's documentation really hard to understand? I can't get a hold on this at all, and I've been at it for hours. If anyone can give me some pointers or a link to some code examples, it would be greatly appreciated. Thanks in advance, guys! Please go easy, I'm just starting out
I'm on Tiger/XCode 2 if it makes any difference...
I really want to intercept/remap keystrokes made by the user into the NSTextField. So if the user types a 'u' character, I want it to come out as a '4' a la a virtual numeric keypad (if that makes sense). I basically want to remap some keys. I also want to intercept some other keystrokes such as the arrow keys.
For the arrow keys, I've managed to implement the control:TextViewoCommandBySelector, which seems to work fine (even though I don't quite know what's going on). But for the keys with actual character values, I just can't seem to get it to work.
After a lot of googling, I think I'm supposed to use
- (BOOL)textViewNSTextView *)textView shouldChangeTextInRangeNSRange)affectedCharRange replacementStringNSString *)replacementString
delegate method, but this doesn't work for me. In this case, I don't even know what the replacementString and affectedCharRange variables represent. Do I just ignore them? I'm not sure this is even what I want though - I just want a simple key remap.
Plus, this function appears to apply to an NSTextView, not an NSTextField - I think there's some overlap between these two classes, but it's all just serving to confuse me. I've also played around with other methods, such as insertText, but I don't quite know what I'm doing exactly.
Is it just me or is Apple's documentation really hard to understand? I can't get a hold on this at all, and I've been at it for hours. If anyone can give me some pointers or a link to some code examples, it would be greatly appreciated. Thanks in advance, guys! Please go easy, I'm just starting out
I'm on Tiger/XCode 2 if it makes any difference...