title: CGEventSetFlags - Discover when shift modifier key is required from local string
body: <p>this is my first post so please let me know if anything needs editing.</p>
  
  <p>I currently have a function that will return the virtual key code of an Apple keyboard by passing it a char; <code>CGKeyCode keyCodeForChar()</code>, it works and I have checked the results against various web sources.</p>
  
  <p>A local string such as <code>NSString *myString = @"Hello World!"</code>, is broken down and each char processed, CGKeyCode retrieved and attempted to be written using <code>CGEventPost()</code>. This works, however in my attempt to detect the correct flags for when a letter should be capitalised, the entire string is printed out as lower case. This is my code so far:</p>
  
  <pre><code>NSString *myString = @"Hello World!";
  if (![self.view.window isKeyWindow])
  {
      for (int i = 0; i &lt; myString.length; i++)
      {
          char c = [theString characterAtIndex:i];
          CGKeyCode keyCode = keyCodeForChar(c);
  
          CGEventRef event;
          CGEventFlags flags = kCGEventFlagMaskShift;
          CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
  
          // Key down event
          event = CGEventCreateKeyboardEvent(source, keyCode, true);
          CGEventSetFlags(event, (flags &amp; CGEventGetFlags(event)));
          CGEventPost(kCGHIDEventTap, event);
          CFRelease(event);
  
          // Key up event
          event = CGEventCreateKeyboardEvent(source, keyCode, false);
          CGEventSetFlags(event, (flags &amp; CGEventGetFlags(event))); 
          CGEventPost(kCGHIDEventTap, event);
          CFRelease(event);
  
          CFRelease(source);
      }
  }
  </code></pre>
  
  <p>Currently the output is <code>hello world1</code>, the 1 coming from the same CGKeyCode as '!'. Therefore I believe my detecting of the correct flag mask is wrong. I have looked at similar questions asked here such as:</p>
  
  <p><a href="https://stackoverflow.com/questions/4420995/obtaining-modifier-key-pressed-in-cgevent-tap">Obtaining modifier key pressed in CGEvent tap</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2008126/cgeventpost-possible-bug-when-simulating-keyboard-events">CGEventPost - possible bug when simulating keyboard events?</a></p>
  
  <p>However the solutions there haven't worked for me, if anyone could help me with where I am going wrong that would be brilliant!</p>
  
  <p>Thank you,
  Craig</p>