void IOHIKeyboard::setNumLock(bool val)
{
    _numLock = val;

    setNumLockFeedback(val);

    KeyboardReserved *tempReservedStruct = GetKeyboardReservedStructEventForService(this); 
	
    if (tempReservedStruct && tempReservedStruct->keyboardNub )
        tempReservedStruct->keyboardNub->setNumLockLEDElement(val);

}
void ApplePS2Keyboard::receiveMouseTouchpadNotifications(UInt32 data)
{
    AbsoluteTime now;
    
    clock_get_uptime((uint64_t *)&now);
    
    switch (data) {
        
        
        case kPS2C_SwipeLeft:// cmd + [
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 33,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 33,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
        case kPS2C_SwipeRight://cmd + ]
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 30,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 30,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
        case kPS2C_ZoomPlus://cmd + +
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 69,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 69,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
        case kPS2C_ZoomMinus://cmd + -
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 78,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 78,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
        case kPS2C_SwipeDown://cmd + tab
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 48,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 48,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
        case  kPS2C_SwipeUp://Launchpad
            dispatchKeyboardEvent( 131,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 131,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
        case kPS2C_RotateLeft://cmd + L
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 37,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            //clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 37,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            //clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );

            break;
        case kPS2C_RotateRight://cmd + R
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 15,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            // clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 15,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            //clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );

            break;
            case kPS2C_TwoFingersPress://cmd + 0
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 29,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 29,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
            case kPS2C_ReleaseKey://Just to release cmd after sleep and after swipe down
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            
            break;
          
        //If Command Key was modified at Modifier keys of Keyboard pane then we set command key
        case kCommandKeyPos_0:
            _commandKey = 55;
            break;
        case kCommandKeyPos_1:
            _commandKey = 58;
            break;
        case kCommandKeyPos_2:
            _commandKey = 59;
            break;
        case kCommandKeyPos_3:
            _commandKey = 57;
            break;
            
        //Three Finger Swipe Action Options
        case kPS2C_SwipeAction_1://Switch Applications Switch Tab + cmd
            dispatchKeyboardEvent( _commandKey,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 48,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 48,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
        case kPS2C_SwipeAction_2://Launchpad
            dispatchKeyboardEvent( 131,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 131,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );

            break;
        case kPS2C_SwipeAction_3://Dashboard & Switching Desktop Left Cntrl + Arrow
            dispatchKeyboardEvent( 59,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 123,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 123,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 59,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            break;
            
        case kPS2C_SwipeAction_4://Switching Desktop Left Cntrl + Right Arrow
            dispatchKeyboardEvent( 59,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 124,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 124,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 59,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );

            break;
            

        case kPS2C_SwipeAction_5://Show Application Windows Control + Down Arrow
            dispatchKeyboardEvent( 59,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 125,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 125,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 59,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            

            break;
        case kPS2C_SwipeAction_6://Mission Control
            dispatchKeyboardEvent( 160,
                                  /*direction*/ true,
                                  /*timeStamp*/ now );
            
            clock_get_uptime((uint64_t *)&now);
            dispatchKeyboardEvent( 160,
                                  /*direction*/ false,
                                  /*timeStamp*/ now );
            

            break;
            
            //For Future
        case kPS2C_SwipeAction_7:
            break;
        case kPS2C_SwipeAction_8:
            break;
        case kPS2C_SwipeAction_9:
            break;
        case kPS2C_SwipeAction_10:
            break;
            
        case kPS2C_NumLock:
            //Invoking Num Lock LED
            setNumLockFeedback(_numKeypadLocked);
            _numKeypadLocked = !_numKeypadLocked;
            break;
            
        default:
            break;
    }

    
    return;
}
bool ApplePS2Keyboard::dispatchKeyboardEventWithScancode(UInt8 scanCode)
{
  //
  // Parses the given scan code, updating all necessary internal state, and
  // should a new key be detected, the key event is dispatched.
  //
  // Returns true if a key event was indeed dispatched.
  //

  unsigned int keyCode;
  bool         goingDown;
  AbsoluteTime now;

    //
    //Accidental Input Keys
    //
    switch (scanCode & ~kSC_UpBit)
    {
        case 0X01://ESC
        case 0X0f://Tab
        case 0x3a://CapsLock
        case 0x2a://Shift
        case 0x36://
        case 0x1d://Cntrl
        case 0x5b://Window
        case 0x38://Alt
        case 0x5d://ContextMenu
        case 0x45://NumLock
        case 0x1c://Enter
        case 0x0e://Backspace
        case 0x64://Arrows
        case 0x65://
        case 0x66://
        case 0x67://
            _accidentalInputKeysException = true;
            break;
        case 0x52:/* 0........9 */
        case 0x4f:
        case 0x50:
        case 0x51:
        case 0x4b:
        case 0x4c:
        case 0x4d:
        case 0x47:
        case 0x48:
        case 0x49:
        case 0x53:/* ". / * - + ENTER" */
        case 0x63:
        case 0x37:
        case 0x4a:
        case 0x4e:
        case 0x62:
            _keyPadKeys = true;
            break;
        default:
            break;
    }

  //
  // See if this scan code introduces an extended key sequence.  If so, note
  // it and then return.  Next time we get a key we'll finish the sequence.
  //

  if (scanCode == kSC_Extend)
  {
    _extendCount = 1;
    return false;
  }

  //
  // See if this scan code introduces an extended key sequence for the Pause
  // Key.  If so, note it and then return.  The next time we get a key, drop
  // it.  The next key we get after that finishes the Pause Key sequence.
  //
  // The sequence actually sent to us by the keyboard for the Pause Key is:
  //
  // 1. E1  Extended Sequence for Pause Key
  // 2. 1D  Useless Data, with Up Bit Cleared
  // 3. 45  Pause Key, with Up Bit Cleared
  // 4. E1  Extended Sequence for Pause Key
  // 5. 9D  Useless Data, with Up Bit Set
  // 6. C5  Pause Key, with Up Bit Set
  //
  // The reason items 4 through 6 are sent with the Pause Key is because the
  // keyboard hardware never generates a release code for the Pause Key and
  // the designers are being smart about it.  The sequence above translates
  // to this parser as two separate events, as it should be -- one down key
  // event and one up key event (for the Pause Key).
  //

  if (scanCode == kSC_Pause)
  {
    _extendCount = 2;
    return false;
  }

  //
  // Convert the scan code into a key code.
  //

  if (_extendCount == 0)
  {
    keyCode = scanCode & ~kSC_UpBit;

    // from "The Undocumented PC" chapter 8, The Keyboard System some
    // keyboard scan codes are single byte, some are multi-byte
    // 3023805:  I want to swap alt and windows, since the windows
    // key is located where the alt/option key is on an Apple PowerBook
    // or USB keyboard, and the alt key is where the Apple/Command
    // key is on the PB or USB keyboard. Left alt is a single scan
    // code byte, right alt is a double scan code byte. Left and
    // right windows keys are double bytes.  This is all set by an
    // entry in Info.plist for ApplePS2Keyboard.kext
    switch (keyCode)
    {
      case 0x3A: 
	if (emacsMode == true) {
	  keyCode = 0x60; 
	}
	break;			// caps lock becomes ctrl
      case 0x38: 
	if (macintoshMode == true) {
	  keyCode = 0x70; 
	}
	break;		// left alt becomes left windows
    }
  }
  else
  {
    _extendCount--;
    if (_extendCount)  return false;
      
      
    //
    // Convert certain extended codes on the PC keyboard into single scancodes.
    // Refer to the conversion table in defaultKeymapOfLength.
    //

    switch (scanCode & ~kSC_UpBit)
    {
      // scancodes from running showkey -s (under Linux) for extra keys on keyboard
      case 0x30: keyCode = 0x7d; break;		   // E030 = volume up
      case 0x2e: keyCode = 0x7e; break;		   // E02E = volume down
      case 0x20: keyCode = 0x7f; break;		   // E020 = volume mute

      case 0x5e: keyCode = 0x7c; break;            // E05E = power
      case 0x5f:                                   // E05F = sleep
        keyCode = 0;
        if (!(scanCode & kSC_UpBit))
        {
          IOPMrootDomain * rootDomain = getPMRootDomain();
          if (rootDomain)
            rootDomain->receivePowerNotification( kIOPMSleepNow );
        }
        break;

      case 0x1D: keyCode = 0x60; break;            // ctrl
      case 0x38:             			   // right alt may become right command
	if (macintoshMode == true) {
	  keyCode = 0x71; 
	} else {
	  keyCode = 0x61;			  
	}
	break;
      case 0x1C: keyCode = 0x62; break;            // enter
      case 0x35: keyCode = 0x63; break;            // /
      case 0x48: keyCode = 0x64; break;            // up arrow
      case 0x50: keyCode = 0x65; break;            // down arrow
      case 0x4B: keyCode = 0x66; break;            // left arrow
      case 0x4D: keyCode = 0x67; break;            // right arrow
      case 0x52: keyCode = 0x68; break;            // insert
      case 0x53: keyCode = 0x69; break;            // delete
      case 0x49: keyCode = 0x6A; break;            // page up
      case 0x51: keyCode = 0x6B; break;            // page down
      case 0x47: keyCode = 0x6C; break;            // home
      case 0x4F: keyCode = 0x6D; break;            // end
      case 0x37: keyCode = 0x6E; break;            // PrintScreen
      case 0x45: keyCode = 0x6F; break;            // Pause
      case 0x5B: 				   // left Windows key may become alt
	if (macintoshMode == true) {
	  keyCode = 0x38; 			   // alt 
	} else {
	  keyCode = 0x70;			   // Left command
	}
	break;
      case 0x5d:  				   //right context click key becomes alt (right Windows key may become alt)
	if (macintoshMode == true) {
	  keyCode = 0x61; 			   // alt
	} else {
	  keyCode = 0x71; 			   // Right command
	}
	break;
      //case 0x5D: keyCode = 0x72; break;            // Application
      case 0x2A:             // header or trailer for PrintScreen
      default: return false;
    }
  }

  if (keyCode == 0)  return false;

  //
  // Update our key bit vector, which maintains the up/down status of all keys.
  //

  goingDown = !(scanCode & kSC_UpBit);

  if (goingDown)
  {
    //
    // Verify that this is not an autorepeated key -- discard it if it is.
    //

    if (KBV_IS_KEYDOWN(keyCode, _keyBitVector))  return false;

    KBV_KEYDOWN(keyCode, _keyBitVector);
  }
  else
  {
    KBV_KEYUP(keyCode, _keyBitVector);
  }

  //
  // We have a valid key event -- dispatch it to our superclass.
  //
    
  //IOLog("Keyboard key Code %x %d\n",keyCode,keyCode);
  //Sending PS2 Notification to Mouse/Touchpad
    if(goingDown)
    if(!_accidentalInputKeysException && !_keyPadKeys)
        _device->dispatchPS2Notification(kPS2C_DisableTouchpad);
    
  clock_get_uptime((uint64_t *)&now);

    //NUM LOCK and Print Screen Fix
    if(keyCode == 0x45 && goingDown)
    {
        setNumLockFeedback(_numKeypadLocked);
        _numKeypadLocked = !_numKeypadLocked;

    }
    else if(keyCode == 0x6e && goingDown)//Print Screen Simulation
    {
        dispatchKeyboardEvent( 55,
                              /*direction*/ true,
                              /*timeStamp*/ now );
        clock_get_uptime((uint64_t *)&now);
        dispatchKeyboardEvent( 56,
                              /*direction*/ true,
                              /*timeStamp*/ now );
        clock_get_uptime((uint64_t *)&now);
        dispatchKeyboardEvent( 21,
                              /*direction*/ true,
                              /*timeStamp*/ now );
        clock_get_uptime((uint64_t *)&now);
        dispatchKeyboardEvent( 21,
                              /*direction*/ false,
                              /*timeStamp*/ now );
        clock_get_uptime((uint64_t *)&now);
        dispatchKeyboardEvent( 56,
                              /*direction*/ false,
                              /*timeStamp*/ now );
        clock_get_uptime((uint64_t *)&now);
        dispatchKeyboardEvent( 55,
                              /*direction*/ false,
                              /*timeStamp*/ now );
    }
    else if(!((keyCode == 0x45 || keyCode == 0x6e) ||//NumLock & PrintScreen
            //Keypad Buttons
            ((keyCode == 0x52 || keyCode == 0x53 || keyCode == 0x62 || keyCode == 0x4f || keyCode == 0x50 ||
            keyCode == 0x51 || keyCode == 0x4b || keyCode == 0x4c || keyCode == 0x4d || keyCode == 0x47 ||
            keyCode == 0x48 || keyCode == 0x49 || keyCode == 0x63 || keyCode == 0x37 || keyCode == 0x4a ||
            keyCode == 0x4e)  && _numKeypadLocked)))
        dispatchKeyboardEvent( PS2ToADBMap[keyCode],  //Dispatch of Keyboard Events
           /*direction*/ goingDown,
           /*timeStamp*/ now );

   
    
    //PS2 Notification
    if(!goingDown && !_accidentalInputKeysException && !_keyPadKeys)
        _device->dispatchPS2Notification(kPS2C_EnableTouchpad);
    
    if(_accidentalInputKeysException)
        _accidentalInputKeysException = false;
    if(_keyPadKeys)
        _keyPadKeys = false;

        /*if(keyCode != 0x2a && keyCode != 0x36 && keyCode != 0x1d && keyCode != 0x60 &&
           keyCode != 0x70 && keyCode != 0x71 && keyCode != 0x38 && keyCode != 0x61 &&
           keyCode != 0x72 && keyCode != 0x0f && keyCode != 0x45 && keyCode != 0x6e && !(keyCode>0x3f && keyCode<0x44))*/
    
  return true;
}