Wednesday, March 18, 2015

python with win32 api

ref: http://sourceforge.net/projects/pywin32/

import win32com.client
import win32api
import win32con
import time

def IsPressed(key):
    # ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646301%28v=vs.85%29.aspx
    return win32api.GetKeyState(key) & 0x8000 == 0x8000

exit = False;
# for tracking key state
prevSpace = 0;
prevUp = 0;
currentSpace = 0;
currentUp = 0;

# loop to track key state
while (exit == False):
    currentSpace = IsPressed(win32con.VK_SPACE);
    currentUp = IsPressed(win32con.VK_UP);
    if IsPressed(win32con.VK_ESCAPE):
       exit = True;
       print "Escape"
    prevSpace = currentSpace;
    prevUp = currentUp;
    time.sleep(0.0001)


# if need to send key to another app
shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("app to activate or send key to ...")
shell.SendKeys("whatever key to send", 0)

python with de facto GUI library. standard library Tk


from Tkinter import *

def bClick():
    t.set(t2.get())

root = Tk()
t = StringVar()
t2 = StringVar()

# create UI elements
label = Label(root, textvariable=t)
entry = Entry(root, textvariable=t2)
b = Button(root, text="Click Me!", command = bClick)

# layout UI elements
label.pack()
entry.pack()
b.pack()

t.set("Hello Label");

root.mainloop()

Thursday, February 05, 2015

Angular JS: how to share variables among different controllers or scope

Using services to create application data to be shared among different controllers or different scopes.
something like a global javascript variable in a single page app. not really session variable as the variable will reset after the page is refreshed.

HTML:

<html data-ng-app="demoApp" >
    <head>
        <title>Angular Demo</title>
    </head>
    <body data-ng-controller="MyController">
        <h1>Hello Angular</h1>
     
        <ul>
            <li><a ng-click="myMethod('clicked')" href="#/SecondPage">second page</a></li>
            <li><a href="#/ThirdPage">third page</a></li>
        </ul>
        <div ng-view></div>
        <script src="angular.min.js"></script>
        <script src="angular-route.min.js"></script>
        <script src="test01.js"></script>
    </body>
</html>


JS:

 var demoApp = angular.module('demoApp', ['ngRoute']);
// add service as dependencies
demoApp.controller('MyController', ['$scope', 'myService', '$route',
                        // add service as parameter
                        function($scope, myService, $route){
    console.log("MyController");
                         
    $scope.mydata = myService; // add service object to scope
 

    $scope.myMethod = function(special){
        console.log("myMethod called: " + special);
        myService.age++;
        myService.phone.price += 0.1;
        myService.addFriend("Friend " + myService.age);
        console.log(myService);
    };
}]);

//Define Routing for app
demoApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/SecondPage', {
        templateUrl: 'second.html',
        controller: 'MyController' // can be different
    }).
      when('/ThirdPage', {
        templateUrl: 'third.html',
        controller: 'MyController' // can be different
      }).
      otherwise({
        redirectTo: '/SecondPage'
      });
}]);

// service
demoApp.factory('myService', function() {
    console.log("service starting");
    // object of this service
  var mydata = {
      // add properties
      name: "My Data",
      age: 30,
      friends: ["Tarth Bader", "Koda", "Kan zolo"],
      phone: {brand: "orange", model: "oPhone Plus", price:2000},
  };
    // add methods
    mydata.addFriend = function(friend){
        this.friends.push(friend);  
    };
  // return service
  return mydata;
});


second.html:
<div>{{ mydata.name }} is of {{mydata.age}}</div>
<ul>
    <li ng-repeat="friend in mydata.friends">{{friend}}</li>
</ul>

Angular JS: how to use separate html to layout the web app

different html to show different UI of the web app. 

HTML: 

<html data-ng-app="demoApp" >
    <head>
        <title>Angular Demo</title>
    </head>
    <body data-ng-controller="MyController">
        <h1>Hello Angular</h1>
        <input type="text" ng-model="name"/> {{name}}
        <div>
            {{age}}
        </div>
        <ul>
            <li><a ng-click="myMethod('clicked')" href="#/SecondPage">second page</a></li>
            <li><a href="#/ThirdPage">third page</a></li>
        </ul>
        <div ng-view><!-- the content from other html file will display here --></div>
        <script src="angular.min.js"></script>
        <script src="angular-route.min.js"></script>
        <script src="test01.js"></script>
    </body>
</html>


JS:

var demoApp = angular.module('demoApp', ['ngRoute']);
demoApp.controller('MyController', function($scope){
    console.log("MyController");
    $scope.hello = "Hello from AngJS";
    $scope.customers = ["dave", "larry"];
    $scope.age = 20;
    
    $scope.message = 'This is a new screen';
    
    $scope.myMethod = function(special){
        console.log("myMethod called: " + special);
        $scope.message = special;
    };
});

//Define Routing for app
demoApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/SecondPage', {
        templateUrl: 'second.html',
        controller: 'MyController' // can be different controller. will instantiate new controller. different scope
    }).
      when('/ThirdPage', {
        templateUrl: 'third.html',
        controller: 'MyController
// can be different controller. will instantiate new controller. different scope
      }).
      otherwise({
        redirectTo: '/SecondPage'
      });
}]);

second.html:

<div>{{message}} </div> <!-- can add other HTML tag -->

third.html:

<div>{{hello}} </div> <!-- can add other HTML tag -->

Angular JS: starting out

basic hello world

HTML: 

<html data-ng-app="demoApp" >
    <head>
        <title>Angular Demo</title>
    </head>
    <body data-ng-controller="MyController">
        <h1>Hello Angular</h1>
        <input type="text" ng-model="name"/> {{name}}
        <div>
            {{age}}
        </div>
        
        <script src="angular.min.js"></script>
        <script src="test01.js"></script>
    </body>
</html>

JS:

var demoApp = angular.module('demoApp', [ ]);
demoApp.controller('MyController', function($scope){
    console.log("MyController");
    $scope.hello = "Hello from AngJS";
    $scope.customers = ["dave", "larry"];
    $scope.age = 20;
    
    $scope.myMethod = function(special){
        console.log("myMethod called: " + special);
        $scope.message = special;
    };
});